From 22529e0ba531f72c8b7016a0fb6191803f7183b1 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sat, 14 Nov 2026 14:08:13 +0100 Subject: [PATCH] Update YieldBreakInCSharp sample to .NET 11 Retarget both projects to net11.0, update the xUnit test stack, use Random.Shared and DateTime.IsLeapYear, add the unreachable-code and finally-block examples, and replace the non-empty tests with behaviour tests. --- .../YieldBreakInCSharp/Tests/Tests.csproj | 10 ++-- .../YieldBreakInCSharp/Tests/UtilityTests.cs | 52 +++++++++++++++--- .../YieldBreakInCSharp/Program.cs | 4 +- .../YieldBreakInCSharp/Utility.cs | 53 ++++++++++++++++--- .../YieldBreakInCSharp.csproj | 2 +- 5 files changed, 99 insertions(+), 22 deletions(-) diff --git a/csharp-basic-topics/YieldBreakInCSharp/Tests/Tests.csproj b/csharp-basic-topics/YieldBreakInCSharp/Tests/Tests.csproj index c9311c1fee..acea135fb9 100644 --- a/csharp-basic-topics/YieldBreakInCSharp/Tests/Tests.csproj +++ b/csharp-basic-topics/YieldBreakInCSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net11.0 enable enable @@ -9,13 +9,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/csharp-basic-topics/YieldBreakInCSharp/Tests/UtilityTests.cs b/csharp-basic-topics/YieldBreakInCSharp/Tests/UtilityTests.cs index bc80dcb5fd..131b1667bc 100644 --- a/csharp-basic-topics/YieldBreakInCSharp/Tests/UtilityTests.cs +++ b/csharp-basic-topics/YieldBreakInCSharp/Tests/UtilityTests.cs @@ -5,22 +5,60 @@ namespace Tests; public class UtilityTests { [Fact] - public void GenerateRandomYears_WhenCalled_GenerateRandomYears() + public void GenerateRandomYears_WhenCalled_StopsAtTheFirstLeapYear() { // Act - var years = Utility.GenerateRandomYears(); + var years = Utility.GenerateRandomYears().ToList(); // Assert - Assert.NotEmpty(years); + Assert.True(DateTime.IsLeapYear(years[^1])); + Assert.All(years.SkipLast(1), year => Assert.False(DateTime.IsLeapYear(year))); } [Fact] - public void GenerateRandomYearsWithBreak_WhenCalled_GenerateRandomYears() + public void GenerateRandomYearsWithUnreachableCode_WhenCalled_SkipsTheLineAfterTheLoop() { // Act - var years = Utility.GenerateRandomYearsWithBreak(); + var output = CaptureConsole(() => Utility.GenerateRandomYearsWithUnreachableCode().ToList()); // Assert - Assert.NotEmpty(years); + Assert.DoesNotContain("executed successfully", output); } -} \ No newline at end of file + + [Fact] + public void GenerateRandomYearsWithBreak_WhenCalled_RunsTheLineAfterTheLoop() + { + // Act + var output = CaptureConsole(() => Utility.GenerateRandomYearsWithBreak().ToList()); + + // Assert + Assert.Contains("executed successfully", output); + } + + [Fact] + public void GenerateRandomYearsWithCleanup_WhenCalled_RunsTheFinallyBlock() + { + // Act + var output = CaptureConsole(() => Utility.GenerateRandomYearsWithCleanup().ToList()); + + // Assert + Assert.Contains("The finally block ran.", output); + } + + private static string CaptureConsole(Action action) + { + var original = Console.Out; + using var writer = new StringWriter(); + Console.SetOut(writer); + try + { + action(); + } + finally + { + Console.SetOut(original); + } + + return writer.ToString(); + } +} diff --git a/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/Program.cs b/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/Program.cs index 0732f71544..1ad02c3f08 100644 --- a/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/Program.cs +++ b/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/Program.cs @@ -1,6 +1,6 @@ using YieldBreakInCSharp; -foreach (int year in Utility.GenerateRandomYearsWithBreak()) +foreach (int year in Utility.GenerateRandomYears()) { Console.WriteLine(year); -} \ No newline at end of file +} diff --git a/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/Utility.cs b/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/Utility.cs index 0c052223d4..cc7d22b38a 100644 --- a/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/Utility.cs +++ b/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/Utility.cs @@ -2,17 +2,15 @@ public static class Utility { - private static readonly Random random = new(); - public static IEnumerable GenerateRandomYears() { int year; while (true) { - year = random.Next(1900, 2023); + year = Random.Shared.Next(1900, 2023); yield return year; - if (year % 4 == 0) + if (DateTime.IsLeapYear(year)) { Console.WriteLine($"{Environment.NewLine}Encountered Leap Year:{year}"); yield break; @@ -20,21 +18,62 @@ public static IEnumerable GenerateRandomYears() } } + public static IEnumerable GenerateRandomYearsWithUnreachableCode() + { + int year; + while (true) + { + year = Random.Shared.Next(1900, 2023); + yield return year; + + if (DateTime.IsLeapYear(year)) + { + Console.WriteLine($"{Environment.NewLine}Encountered Leap Year:{year}"); + yield break; + } + } + + Console.WriteLine("GenerateRandomYears() method executed successfully."); + } + public static IEnumerable GenerateRandomYearsWithBreak() { int year; while (true) { - year = random.Next(1900, 2023); + year = Random.Shared.Next(1900, 2023); yield return year; - if (year % 4 == 0) + if (DateTime.IsLeapYear(year)) { Console.WriteLine($"{Environment.NewLine}Encountered Leap Year:{year}"); break; } } - Console.WriteLine("GenerateRandomYears method executed successfully."); + Console.WriteLine("GenerateRandomYears() method executed successfully."); + } + + public static IEnumerable GenerateRandomYearsWithCleanup() + { + try + { + int year; + while (true) + { + year = Random.Shared.Next(1900, 2023); + yield return year; + + if (DateTime.IsLeapYear(year)) + { + Console.WriteLine($"{Environment.NewLine}Encountered Leap Year:{year}"); + yield break; + } + } + } + finally + { + Console.WriteLine("The finally block ran."); + } } } diff --git a/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/YieldBreakInCSharp.csproj b/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/YieldBreakInCSharp.csproj index f02677bf64..5bf6c9b8cf 100644 --- a/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/YieldBreakInCSharp.csproj +++ b/csharp-basic-topics/YieldBreakInCSharp/YieldBreakInCSharp/YieldBreakInCSharp.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net11.0 enable enable