Skip to content

Commit

Permalink
Added example for static local functions (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkMichaelis authored Aug 31, 2023
1 parent d11ebcb commit 6484ede
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 19 deletions.
17 changes: 17 additions & 0 deletions src/Chapter13.Tests/Listing13.21.StaticAnonymousFunctions.Tests.cs
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" } );
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_21.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_22.Tests;

[TestClass]
public class ProgramTests
Expand All @@ -13,4 +13,4 @@ public void MainTest()
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, Program.Main);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_22.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_23.Tests;

[TestClass]
public class ProgramTests
Expand All @@ -13,4 +13,4 @@ public void MainTest()
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, CaptureLoop.Main);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_23.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_24.Tests;

[TestClass]
public class ProgramTests
Expand All @@ -13,4 +13,4 @@ public void MainTest()
IntelliTect.TestTools.Console.ConsoleAssert.Expect(
expected, DoNotCaptureLoop.Main);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_25.Tests;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_26.Tests;

[TestClass]
public class ProgramTests
Expand Down Expand Up @@ -30,4 +30,4 @@ public void Main_ExamingingAnExpressTree()
Program.Main();
});
}
}
}
50 changes: 50 additions & 0 deletions src/Chapter13/Listing13.21.StaticAnonymousFunctions.cs
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_21;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_22;

using System;
using Listing13_11;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_22;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_23;

using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_23;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_24;

using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_24;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_25;

/*
#region INCLUDE
Expand All @@ -8,4 +8,4 @@
SQL WHERE CLAUSE:
select * from Person where upper(Name) = 'INIGO MONTOYA';
#endregion INCLUDE
*/
*/
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_25;
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter13.Listing13_26;

#region INCLUDE
using System;
Expand Down Expand Up @@ -58,4 +58,4 @@ private static string NodeToString(Expression expression) =>
" (" + expression.NodeType.ToString() + ")",
};
#endregion INCLUDE
}
}

0 comments on commit 6484ede

Please sign in to comment.