From 0b8c41edbbb4e75128b2b377780735a43a4cfff0 Mon Sep 17 00:00:00 2001 From: Code Maze Date: Sun, 7 Nov 2021 17:49:26 +0100 Subject: [PATCH 1/3] CM-46: Delegates in C# Inital Commit --- .../DelegatesInCsharp/DelegatesInCsharp.sln | 31 ++++++ .../DelegatesInCsharp.csproj | 8 ++ .../DelegatesInCsharp/Program.cs | 53 +++++++++++ .../DelegatesInCsharp/Tests/Tests.cs | 94 +++++++++++++++++++ .../DelegatesInCsharp/Tests/Tests.csproj | 16 ++++ 5 files changed, 202 insertions(+) create mode 100644 csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp.sln create mode 100644 csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/DelegatesInCsharp.csproj create mode 100644 csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs create mode 100644 csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs create mode 100644 csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.csproj diff --git a/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp.sln b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp.sln new file mode 100644 index 0000000000..ea1653648e --- /dev/null +++ b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31702.278 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DelegatesInCsharp", "DelegatesInCsharp\DelegatesInCsharp.csproj", "{B7E7E843-3109-4BFF-90DD-B979CA1853B0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{CAAB0C6D-B8F4-487D-8114-8EAA72D02BEF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B7E7E843-3109-4BFF-90DD-B979CA1853B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B7E7E843-3109-4BFF-90DD-B979CA1853B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B7E7E843-3109-4BFF-90DD-B979CA1853B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B7E7E843-3109-4BFF-90DD-B979CA1853B0}.Release|Any CPU.Build.0 = Release|Any CPU + {CAAB0C6D-B8F4-487D-8114-8EAA72D02BEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CAAB0C6D-B8F4-487D-8114-8EAA72D02BEF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CAAB0C6D-B8F4-487D-8114-8EAA72D02BEF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CAAB0C6D-B8F4-487D-8114-8EAA72D02BEF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {84A17409-6852-4689-A48E-AE3682E9D105} + EndGlobalSection +EndGlobal diff --git a/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/DelegatesInCsharp.csproj b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/DelegatesInCsharp.csproj new file mode 100644 index 0000000000..1d2d39a9ef --- /dev/null +++ b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/DelegatesInCsharp.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + + diff --git a/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs new file mode 100644 index 0000000000..f0a2d10334 --- /dev/null +++ b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace DelegatesInCsharp +{ + delegate void PrintMessage(string text); + delegate T Print(T param1); + + class Program + { + public static void WriteText(string text) { Console.WriteLine($"Text: {text}"); } + public static void ReverseWriteText(string text) { Console.WriteLine($"Text in reverse: {Reverse(text)}"); } + public static string ReverseText(string text) { return Reverse(text); } + + private static string Reverse(string s) + { + char[] charArray = s.ToCharArray(); + Array.Reverse(charArray); + return new string(charArray); + } + + static void Main(string[] args) + { + PrintMessage delegate1 = new PrintMessage(WriteText); + PrintMessage delegate2 = new PrintMessage(ReverseWriteText); + // with + sign + PrintMessage multicastDelegate = delegate1 + delegate2; + + // with =, +=, and -= + multicastDelegate = delegate1; + multicastDelegate += delegate2; + + multicastDelegate.Invoke("Go ahead, make my day."); + multicastDelegate("You're gonna need a bigger boat."); + + Print delegate3 = new Print(ReverseText); + Console.WriteLine(delegate3("I'll be back.")); + + Action executeReverseWrite = ReverseWriteText; + executeReverseWrite("You're gonna need a bigger boat."); + + Func executeReverse = ReverseText; + Console.WriteLine(executeReverse("You're gonna need a bigger boat.")); + + // comment out other stuff + Action executeReverseWriteAction = ReverseWriteText; + executeReverseWriteAction("Are you not entertained?"); + Func executeReverseFunc = ReverseText; + Console.WriteLine(executeReverse("Are you not entertained?")); + } + } +} diff --git a/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs new file mode 100644 index 0000000000..447a2a2f01 --- /dev/null +++ b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs @@ -0,0 +1,94 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace Tests +{ + delegate string PrintMessage(string text); + delegate T Print(T param1); + + [TestClass] + public class Tests + { + public static string WriteText(string text) { return $"Text:{text}"; } + public static string ReverseText(string text) { return Reverse(text); } + public static void ReverseWriteText(string text) { Console.WriteLine(Reverse(text)); } + + private static string Reverse(string s) + { + char[] charArray = s.ToCharArray(); + Array.Reverse(charArray); + return new string(charArray); + } + + [TestMethod] + public void whenStringIsSent_DelegateExecutesTheReferencedMethod() + { + PrintMessage delegate1 = new PrintMessage(WriteText); + var result = delegate1("You're gonna need a bigger boat."); + Assert.AreEqual("Text:You're gonna need a bigger boat.", result); + } + + [TestMethod] + public void whenStringIsSent_DelegateReturnsTheReversedString() + { + PrintMessage delegate1 = new PrintMessage(ReverseText); + var result = delegate1("You're gonna need a bigger boat."); + Assert.AreEqual(Reverse("You're gonna need a bigger boat."), result); + } + + [TestMethod] + public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusSign_DelegateInvocationListContainsTwoMethods() + { + PrintMessage delegate1 = new PrintMessage(WriteText); + PrintMessage delegate2 = new PrintMessage(ReverseText); + PrintMessage multicastDelegate = delegate1 + delegate2; + + var invocationList = multicastDelegate.GetInvocationList(); + + Assert.AreEqual(invocationList.Length, 2); + Assert.AreEqual(invocationList[0].Method.Name, "WriteText"); + Assert.AreEqual(invocationList[1].Method.Name, "ReverseText"); + } + + [TestMethod] + public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusEquals_DelegateInvocationListContainsTwoMethods() + { + PrintMessage delegate1 = new PrintMessage(WriteText); + PrintMessage delegate2 = new PrintMessage(ReverseText); + PrintMessage multicastDelegate = delegate1; + multicastDelegate += delegate2; + + var invocationList = multicastDelegate.GetInvocationList(); + + Assert.AreEqual(invocationList.Length, 2); + Assert.AreEqual(invocationList[0].Method.Name, "WriteText"); + Assert.AreEqual(invocationList[1].Method.Name, "ReverseText"); + } + + [TestMethod] + public void whenGenericDelegate_DelegateExecutesTheReferencedMethod() + { + Print delegate1 = new Print(ReverseText); + + var result = delegate1("You're gonna need a bigger boat."); + + Assert.AreEqual(Reverse("You're gonna need a bigger boat."), result); + } + + [TestMethod] + public void whenActionDelegate_DelegateInvocationListNotEmpty() + { + Action executeReverseWriteAction = ReverseWriteText; + var invocationList = executeReverseWriteAction.GetInvocationList(); + Assert.AreEqual(invocationList.Length, 1); + } + + [TestMethod] + public void whenFuncDelegate_DelegateInvocationListNotEmpty() + { + Func executeReverseWriteAction = ReverseText; + var invocationList = executeReverseWriteAction.GetInvocationList(); + Assert.AreEqual(invocationList.Length, 1); + } + } +} diff --git a/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.csproj b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.csproj new file mode 100644 index 0000000000..4203089904 --- /dev/null +++ b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.csproj @@ -0,0 +1,16 @@ + + + + net5.0 + + false + + + + + + + + + + From ae942072a27e6e353bd9ccbd91e3bcc8ce77cc89 Mon Sep 17 00:00:00 2001 From: Code Maze Date: Mon, 8 Nov 2021 15:11:03 +0100 Subject: [PATCH 2/3] Cleaning --- .../DelegatesInCsharp/Program.cs | 26 +++++++------------ .../DelegatesInCsharp/Tests/Tests.cs | 18 ++++++------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs index f0a2d10334..f928217476 100644 --- a/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs +++ b/csharp-advanced-topics/DelegatesInCsharp/DelegatesInCsharp/Program.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; namespace DelegatesInCsharp { @@ -9,9 +7,9 @@ namespace DelegatesInCsharp class Program { - public static void WriteText(string text) { Console.WriteLine($"Text: {text}"); } - public static void ReverseWriteText(string text) { Console.WriteLine($"Text in reverse: {Reverse(text)}"); } - public static string ReverseText(string text) { return Reverse(text); } + public static void WriteText(string text) => Console.WriteLine($"Text: {text}"); + public static void ReverseWriteText(string text) => Console.WriteLine($"Text in reverse: {Reverse(text)}"); + public static string ReverseText(string text) => Reverse(text); private static string Reverse(string s) { @@ -22,10 +20,10 @@ private static string Reverse(string s) static void Main(string[] args) { - PrintMessage delegate1 = new PrintMessage(WriteText); - PrintMessage delegate2 = new PrintMessage(ReverseWriteText); + var delegate1 = new PrintMessage(WriteText); + var delegate2 = new PrintMessage(ReverseWriteText); // with + sign - PrintMessage multicastDelegate = delegate1 + delegate2; + var multicastDelegate = delegate1 + delegate2; // with =, +=, and -= multicastDelegate = delegate1; @@ -34,20 +32,14 @@ static void Main(string[] args) multicastDelegate.Invoke("Go ahead, make my day."); multicastDelegate("You're gonna need a bigger boat."); - Print delegate3 = new Print(ReverseText); + var delegate3 = new Print(ReverseText); Console.WriteLine(delegate3("I'll be back.")); - Action executeReverseWrite = ReverseWriteText; - executeReverseWrite("You're gonna need a bigger boat."); - - Func executeReverse = ReverseText; - Console.WriteLine(executeReverse("You're gonna need a bigger boat.")); - // comment out other stuff Action executeReverseWriteAction = ReverseWriteText; executeReverseWriteAction("Are you not entertained?"); - Func executeReverseFunc = ReverseText; - Console.WriteLine(executeReverse("Are you not entertained?")); + Func executeReverseFunc = ReverseText; + Console.WriteLine(executeReverseFunc("Are you not entertained?")); } } } diff --git a/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs index 447a2a2f01..ce693d6792 100644 --- a/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs +++ b/csharp-advanced-topics/DelegatesInCsharp/Tests/Tests.cs @@ -23,7 +23,7 @@ private static string Reverse(string s) [TestMethod] public void whenStringIsSent_DelegateExecutesTheReferencedMethod() { - PrintMessage delegate1 = new PrintMessage(WriteText); + var delegate1 = new PrintMessage(WriteText); var result = delegate1("You're gonna need a bigger boat."); Assert.AreEqual("Text:You're gonna need a bigger boat.", result); } @@ -31,7 +31,7 @@ public void whenStringIsSent_DelegateExecutesTheReferencedMethod() [TestMethod] public void whenStringIsSent_DelegateReturnsTheReversedString() { - PrintMessage delegate1 = new PrintMessage(ReverseText); + var delegate1 = new PrintMessage(ReverseText); var result = delegate1("You're gonna need a bigger boat."); Assert.AreEqual(Reverse("You're gonna need a bigger boat."), result); } @@ -39,9 +39,9 @@ public void whenStringIsSent_DelegateReturnsTheReversedString() [TestMethod] public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusSign_DelegateInvocationListContainsTwoMethods() { - PrintMessage delegate1 = new PrintMessage(WriteText); - PrintMessage delegate2 = new PrintMessage(ReverseText); - PrintMessage multicastDelegate = delegate1 + delegate2; + var delegate1 = new PrintMessage(WriteText); + var delegate2 = new PrintMessage(ReverseText); + var multicastDelegate = delegate1 + delegate2; var invocationList = multicastDelegate.GetInvocationList(); @@ -53,9 +53,9 @@ public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusSign_DelegateIn [TestMethod] public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusEquals_DelegateInvocationListContainsTwoMethods() { - PrintMessage delegate1 = new PrintMessage(WriteText); - PrintMessage delegate2 = new PrintMessage(ReverseText); - PrintMessage multicastDelegate = delegate1; + var delegate1 = new PrintMessage(WriteText); + var delegate2 = new PrintMessage(ReverseText); + var multicastDelegate = delegate1; multicastDelegate += delegate2; var invocationList = multicastDelegate.GetInvocationList(); @@ -68,7 +68,7 @@ public void givenMulticastDelegate_whenTwoReferencedMethodAndPlusEquals_Delegate [TestMethod] public void whenGenericDelegate_DelegateExecutesTheReferencedMethod() { - Print delegate1 = new Print(ReverseText); + var delegate1 = new Print(ReverseText); var result = delegate1("You're gonna need a bigger boat."); From 8a3f99f2b0bc52938956012a9b498cd6e4c2dd03 Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Mon, 10 Aug 2026 02:30:14 +0200 Subject: [PATCH 3/3] compare-datetime: update for .NET 10 rewrite Retarget sample and tests to net10.0; simplify IsDateInSameTimeZone() and IsDatePrecisionSame() to direct boolean returns; add a CompareWithTolerance() example demonstrating tolerance-based and DateOnly date comparisons. --- .../CompareDateTimeInCSharp.csproj | 2 +- .../CompareDateTimeInCSharp/Examples.cs | 26 +++++++++++-------- .../CompareDateTimeInCSharp/Program.cs | 6 +++++ .../Tests/Tests.csproj | 2 +- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/CompareDateTimeInCSharp.csproj b/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/CompareDateTimeInCSharp.csproj index f02677bf64..dfb40caafc 100644 --- a/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/CompareDateTimeInCSharp.csproj +++ b/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/CompareDateTimeInCSharp.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net10.0 enable enable diff --git a/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/Examples.cs b/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/Examples.cs index d17c69ac41..f7590a2790 100644 --- a/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/Examples.cs +++ b/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/Examples.cs @@ -8,12 +8,7 @@ public static bool IsDateInSameTimeZone() var secondDate = new DateTime(2021, 05, 06, 12, 0, 0, DateTimeKind.Utc); var firstDateAsUtc = firstDate.ToUniversalTime(); - if (firstDateAsUtc.Equals(secondDate)) - { - return true; - } - - return false; + return firstDateAsUtc.Equals(secondDate); } public static bool IsDatePrecisionSame() @@ -21,12 +16,21 @@ public static bool IsDatePrecisionSame() var firstDate = new DateTime(2021, 05, 06, 12, 0, 0); var secondDate = new DateTime(2021, 05, 06, 12, 0, 0, 500); - if (firstDate == secondDate) - { - return true; - } + return firstDate == secondDate; + } + + public static (bool AreClose, bool SameDay, bool SameDayViaDateOnly) CompareWithTolerance() + { + var firstDate = new DateTime(2021, 05, 06, 12, 0, 0); + var secondDate = new DateTime(2021, 05, 06, 12, 0, 0, 500); + + var tolerance = TimeSpan.FromMilliseconds(1); + var areClose = (firstDate - secondDate).Duration() <= tolerance; + + var sameDay = firstDate.Date == secondDate.Date; + var sameDayViaDateOnly = DateOnly.FromDateTime(firstDate) == DateOnly.FromDateTime(secondDate); - return false; + return (areClose, sameDay, sameDayViaDateOnly); } } } diff --git a/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/Program.cs b/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/Program.cs index a35515f33f..98154d6c45 100644 --- a/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/Program.cs +++ b/dotnet-datetime/CompareDateTimeInCSharp/CompareDateTimeInCSharp/Program.cs @@ -46,6 +46,12 @@ static void Main(string[] args) { Console.WriteLine("Date precisions are different."); } + + // Comparing DateTime values with a tolerance, and comparing by date only + var (areClose, sameDay, sameDayViaDateOnly) = Examples.CompareWithTolerance(); + Console.WriteLine($"Within 1ms tolerance: {areClose}"); + Console.WriteLine($"Same day (Date property): {sameDay}"); + Console.WriteLine($"Same day (DateOnly): {sameDayViaDateOnly}"); } private static void ShowResult(DateTime firstDate, DateTime secondDate, int result) diff --git a/dotnet-datetime/CompareDateTimeInCSharp/Tests/Tests.csproj b/dotnet-datetime/CompareDateTimeInCSharp/Tests/Tests.csproj index 5e9fe2925b..519832e760 100644 --- a/dotnet-datetime/CompareDateTimeInCSharp/Tests/Tests.csproj +++ b/dotnet-datetime/CompareDateTimeInCSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable