-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example for static local functions (#530)
- Loading branch information
1 parent
d11ebcb
commit 6484ede
Showing
11 changed files
with
86 additions
and
19 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/Chapter13.Tests/Listing13.21.StaticAnonymousFunctions.Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using AddisonWesley.Michaelis.EssentialCSharp.Shared.Tests; | ||
|
||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_21.Tests; | ||
|
||
[TestClass] | ||
public class ProgramTests | ||
{ | ||
[TestMethod] | ||
public async Task StaticAnonymousFunctionBehavior() | ||
{ | ||
await CompilerAssert.CompileAsync( | ||
new string[] { "Listing13.21.StaticAnonymousFunctions.cs", | ||
"Listing13.11.UsingADifferentFuncCompatibleMethod.cs"}, | ||
new string[] { "CS8820" } ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_21; | ||
|
||
using System; | ||
using Listing13_11; | ||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
int[] items = new int[5]; | ||
|
||
for (int i = 0; i < items.Length; i++) | ||
{ | ||
Console.Write("Enter an integer:"); | ||
string? text = Console.ReadLine(); | ||
if (!int.TryParse(text, out items[i])) | ||
{ | ||
Console.WriteLine($"'{text}' is not a valid integer."); | ||
return; | ||
} | ||
} | ||
|
||
#region INCLUDE | ||
int comparisonCount = 0; | ||
|
||
DelegateSample.BubbleSort(items, | ||
#region HIGHLIGHT | ||
static (int first, int second) => | ||
#endregion HIGHLIGHT | ||
{ | ||
#if COMPILEERROR // EXCLUDE | ||
// Error CS8820: A static anonymous function | ||
// cannot contain a reference to comparisonCount. | ||
comparisonCount++; | ||
#endif // COMPILEERROR EXCLUDE | ||
return first < second; | ||
} | ||
); | ||
|
||
for (int i = 0; i < items.Length; i++) | ||
{ | ||
Console.WriteLine(items[i]); | ||
} | ||
|
||
#region HIGHLIGHT | ||
Console.WriteLine("Items were compared {0} times.", | ||
comparisonCount); | ||
#endregion HIGHLIGHT | ||
} | ||
} | ||
#endregion INCLUDE |
2 changes: 1 addition & 1 deletion
2
...deGeneratedByCompilerForOuterVariables.cs → ...deGeneratedByCompilerForOuterVariables.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...13.22.CapturingLoopVariablesCSharpFive.cs → ...13.23.CapturingLoopVariablesCSharpFive.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...LoopVariableWorkaroundBeforeCSharpFive.cs → ...LoopVariableWorkaroundBeforeCSharpFive.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters