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 61d399584319d43a2a61d86da6827cd9f5f7626e Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Tue, 11 Aug 2026 11:26:10 +0200 Subject: [PATCH 3/3] RestVsWebSocket: retarget net10.0, drop stale guard, Thread.Sleep to Task.Delay --- .../RestVsWebSocket/Client/Client.csproj | 2 +- .../Controllers/TaskController.cs | 32 ++++--------------- .../RestVsWebSocket/Program.cs | 22 +++---------- .../RestVsWebSocket/RestVsWebSocket.csproj | 5 ++- .../RestVsWebSocket/Tests/Tests.csproj | 2 +- 5 files changed, 15 insertions(+), 48 deletions(-) diff --git a/aspnetcore-webapi/RestVsWebSocket/Client/Client.csproj b/aspnetcore-webapi/RestVsWebSocket/Client/Client.csproj index f02677bf64..dfb40caafc 100644 --- a/aspnetcore-webapi/RestVsWebSocket/Client/Client.csproj +++ b/aspnetcore-webapi/RestVsWebSocket/Client/Client.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net10.0 enable enable diff --git a/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/Controllers/TaskController.cs b/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/Controllers/TaskController.cs index 13db6a2ea3..7f8c3ad223 100644 --- a/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/Controllers/TaskController.cs +++ b/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/Controllers/TaskController.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Mvc; using System.Net.WebSockets; -using System.Net; using System.Text; namespace RestVsWebSocket.Controllers; @@ -35,32 +34,15 @@ public IActionResult AddTask([FromBody] string task) [HttpGet] public async Task Get() { - if (HttpContext.WebSockets.IsWebSocketRequest) - { - using var ws = await HttpContext.WebSockets.AcceptWebSocketAsync(); + using var ws = await HttpContext.WebSockets.AcceptWebSocketAsync(); - while (true) - { - var message = "The time is: " + DateTime.Now.ToString("HH:mm:ss"); - var bytes = Encoding.UTF8.GetBytes(message); - var arraySegment = new ArraySegment(bytes, 0, bytes.Length); - if (ws.State == WebSocketState.Open) - { - await ws.SendAsync(arraySegment, - WebSocketMessageType.Text, - true, - CancellationToken.None); - } - else if (ws.State == WebSocketState.Closed || ws.State == WebSocketState.Aborted) - { - break; - } - Thread.Sleep(1000); - } - } - else + while (ws.State == WebSocketState.Open) { - HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest; + var message = $"The time is: {DateTime.Now:HH:mm:ss}"; + var bytes = Encoding.UTF8.GetBytes(message); + + await ws.SendAsync(bytes, WebSocketMessageType.Text, true, CancellationToken.None); + await Task.Delay(1000); } } } \ No newline at end of file diff --git a/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/Program.cs b/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/Program.cs index eaf9e15c7b..b2c874c22a 100644 --- a/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/Program.cs +++ b/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/Program.cs @@ -1,28 +1,14 @@ var builder = WebApplication.CreateBuilder(args); -// Add services to the container. - builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddOpenApi(); -builder.WebHost.UseUrls("http://localhost:5289"); +builder.WebHost.UseUrls("http://localhost:5289"); // Client/Program.cs hardcodes this URL var app = builder.Build(); -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) -{ - app.UseSwagger(); - app.UseSwaggerUI(); -} - -app.UseHttpsRedirection(); - -app.UseAuthorization(); - -app.MapControllers(); +app.MapOpenApi(); app.UseWebSockets(); +app.MapControllers(); app.Run(); diff --git a/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/RestVsWebSocket.csproj b/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/RestVsWebSocket.csproj index d2f5cb08bd..e1f290f996 100644 --- a/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/RestVsWebSocket.csproj +++ b/aspnetcore-webapi/RestVsWebSocket/RestVsWebSocket/RestVsWebSocket.csproj @@ -1,14 +1,13 @@ - net7.0 + net10.0 enable enable - - + diff --git a/aspnetcore-webapi/RestVsWebSocket/Tests/Tests.csproj b/aspnetcore-webapi/RestVsWebSocket/Tests/Tests.csproj index 9b74c13fbb..c5f74c4801 100644 --- a/aspnetcore-webapi/RestVsWebSocket/Tests/Tests.csproj +++ b/aspnetcore-webapi/RestVsWebSocket/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable