# Links für PH-Seminar "Was gibt es Neues in C# und .NET" ## Organisatorisches * [Anwesenheitsliste 28.11.](https://forms.microsoft.com/e/yJ5d1PpxxE) * [Anwesenheitsliste 29.11.](https://forms.microsoft.com/e/5E3AkeVKrA) ## Linkliste * [Slidesammlung](https://slides.com/rainerstropek) * [Beispiel für Records](https://github.com/rstropek/Samples/tree/master/CSharp10/RecordsInsideOut) * [Rainer's C# Unterlagen für die AnfängerInnen](https://htl-leo-prog-1.github.io/programming_fundamentals_cs/) * ["Fun with Trees" Beispiel](https://github.com/rstropek/Samples/blob/master/CSharp11/FunWithTrees/Program.cs) * [MS Docs für Minimal API](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis) * [Fluentvalidation](https://docs.fluentvalidation.net/en/latest/) * [Beispiel Todo-API von David Fowler](https://github.com/davidfowl/TodoApi) * [Video: Learning to Love Legacy Code](https://youtu.be/wPjHuvulivM?t=1964) * [Video: Why web tech is like this](https://www.youtube.com/watch?v=3QEoJRjxnxQ&t=2s) * [Beispiel: Pattern Matching Inside Out](https://github.com/rstropek/Samples/blob/master/CSharp11/PatternMatchingInsideOut/Program.cs) * [Isometric Game](https://github.com/rstropek/Samples/tree/master/CSharp11/IsometricGame) ## Online C# Entwicklungsumgebungen * [sharplab.io](https://sharplab.io) * [.NET Fiddle](https://dotnetfiddle.net/) * [VSCode Share Link](https://prod.liveshare.vsengsaas.visualstudio.com/join?87F36D7DCDD27426693294E5D39D452CC863) ## Lambda-Wiederholung ```csharp // System.Console.WriteLine(Add(1, 2)); // System.Console.WriteLine(Subtract(1, 2)); CalculateAndPrint(1, 2, Add); CalculateAndPrint(1, 2, Subtract); CalculateAndPrint(1, 2, (x, y) => x - y); CalculateAndPrint(1, 2, (x, y) => x * y); CalculateAndPrint(1, 2, (x, y) => x / y); CalculateAndPrint(1.0, 2.0, (x, y) => x / y); CalculateAndPrint("Hello ", "World", (x, y) => x + y); // Strategy Pattern void CalculateAndPrint<T>(T x, T y, Func<T, T, T> f) { var result = f(x, y); System.Console.WriteLine(result); } Filter(new [] { 1, 2, 3, 4, 5 }, x => x % 2 == 0); Filter(new [] { "Tom", "Tim", "Hans" }, x => x.StartsWith("T")); IEnumerable<T> Filter<T>(IEnumerable<T> source, Func<T, bool> predicate) { foreach (var item in source) { if (predicate(item)) { yield return item; } } } int Add(int x, int y) { return x + y; } int Subtract(int x, int y) { return x - y; } ``` ## Minimal API Mini-Beispiel ```csharp using Microsoft.AspNetCore.Mvc; var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.UseHttpsRedirection(); app.MapGet("/ping", () => "pong"); app.MapGet("/calc", (int a, int b) => a + b); app.MapPost("/calc", (CalcRequestDto param) => new CalcResponseDto(param.A + param.B)); app.MapPost("/calc-advanced", (CalcAdvancedRequestDto param) => { if (param.Operator == "/" && param.B == 0) { return Results.BadRequest(new ProblemDetails() { Type = "https://errors.com/dbz", Title = "Divide by zero" }); } return Results.Ok(new CalcResponseDto(param.Operator switch { "+" => param.A + param.B, "-" => param.A - param.B, "*" => param.A * param.B, "/" => param.A / param.B, _ => 0 })); }); app.Run(); record CalcRequestDto(int A, int B); record CalcAdvancedRequestDto(int A, int B, string Operator); record CalcResponseDto(int Result); ``` ## `IAsyncEnumerable` Beispiel ```csharp await foreach(var n in GetNumbers(10)) { System.Console.WriteLine(n); } async IAsyncEnumerable<int> GetNumbers(int max) { for(var i = 0; i < max; i++) { var val = await ReadNumberFromSensor(); yield return val; } } Task<int> ReadNumberFromSensor() { return Task.FromResult(42); } ```