developpement:dotnet:csharp:introduction
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
developpement:dotnet:csharp:introduction [2023/10/05 15:02] – [PLINQ] sgariepy | developpement:dotnet:csharp:introduction [2023/10/06 05:06] (Version actuelle) – [Reactive] sgariepy | ||
---|---|---|---|
Ligne 1486: | Ligne 1486: | ||
</ | </ | ||
+ | ==== Degree of Parallelism ==== | ||
+ | |||
+ | <code csharp> | ||
+ | void Main() | ||
+ | { | ||
+ | List< | ||
+ | websites.Add(" | ||
+ | websites.Add(" | ||
+ | websites.Add(" | ||
+ | | ||
+ | List< | ||
+ | .AsParallel() | ||
+ | .WithDegreeOfParallelism(websites.Count()) | ||
+ | .Select(PingSites) | ||
+ | .ToList(); | ||
+ | | ||
+ | foreach (var response in responses) | ||
+ | { | ||
+ | Console.WriteLine(response.Address + " " + response.Status + " " + response.RoundtripTime); | ||
+ | } | ||
+ | | ||
+ | Console.ReadLine(); | ||
+ | } | ||
+ | |||
+ | private static PingReply PingSites(string websiteName) | ||
+ | { | ||
+ | Ping ping = new Ping(); | ||
+ | return ping.Send(websiteName); | ||
+ | } | ||
+ | </ | ||
Ligne 1494: | Ligne 1524: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | |||
+ | |||
+ | ====== Pattern Matching ====== | ||
+ | |||
+ | <code csharp> | ||
+ | void Main() | ||
+ | { | ||
+ | var circle = new Circle(5); | ||
+ | var circleRadius100 = new Circle(250); | ||
+ | var rectangle = new Rectangle(420, | ||
+ | var square = new Rectangle(70, | ||
+ | |||
+ | var shapes = new List< | ||
+ | |||
+ | var randomShape = shapes[new Random().Next(shapes.Count)]; | ||
+ | |||
+ | CSharp6Feature(randomShape); | ||
+ | CSharp7Feature(randomShape); | ||
+ | CSharp8Feature(randomShape); | ||
+ | CSharp9Feature(randomShape); | ||
+ | } | ||
+ | |||
+ | private void CSharp6Feature(Shape shape) | ||
+ | { | ||
+ | Console.WriteLine(" | ||
+ | if (shape is Circle) // ' | ||
+ | { | ||
+ | var circle = (Circle)shape; | ||
+ | Console.WriteLine($" | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Console.WriteLine($" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private void CSharp7Feature(Shape shape) | ||
+ | { | ||
+ | Console.WriteLine(" | ||
+ | |||
+ | if (shape is Circle circle) | ||
+ | { | ||
+ | Console.WriteLine($" | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Console.WriteLine($" | ||
+ | } | ||
+ | | ||
+ | // using switch | ||
+ | | ||
+ | switch (shape) | ||
+ | { | ||
+ | case Circle c: | ||
+ | Console.WriteLine($" | ||
+ | break; | ||
+ | case Rectangle r when r.Height == r.Width: | ||
+ | Console.WriteLine($" | ||
+ | break; | ||
+ | default: | ||
+ | Console.WriteLine($" | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | private void CSharp8Feature(Shape shape) | ||
+ | { | ||
+ | Console.WriteLine(" | ||
+ | |||
+ | if (shape is Circle { Radius: 10 }) | ||
+ | { | ||
+ | Console.WriteLine($" | ||
+ | } | ||
+ | | ||
+ | var shapeDetails = shape switch | ||
+ | { | ||
+ | Circle => "This is a circle", | ||
+ | Rectangle rec when rec.Height == rec.Width => "This is a square", | ||
+ | _ => "Shape is something else" | ||
+ | }; | ||
+ | } | ||
+ | |||
+ | private void CSharp9Feature(Shape shape) | ||
+ | { | ||
+ | Console.WriteLine(" | ||
+ | |||
+ | if (shape is not Rectangle) // ' | ||
+ | { | ||
+ | Console.WriteLine($" | ||
+ | } | ||
+ | |||
+ | if (shape is Circle { Radius: > 100 and < 200, Area: >= 1000 }) | ||
+ | { | ||
+ | Console.WriteLine($" | ||
+ | } | ||
+ | |||
+ | |||
+ | // that can be used like so: if (shape is not null) {...} | ||
+ | |||
+ | var shapeDetails = shape switch | ||
+ | { | ||
+ | Circle => "This is a circle", | ||
+ | Rectangle rec when rec.Height == rec.Width => "This is a square", | ||
+ | { Area: 100 } => "Area is 100", | ||
+ | _ => "Shape is something else" | ||
+ | }; | ||
+ | |||
+ | var areaDetails = shape.Area switch | ||
+ | { | ||
+ | >= 100 and <= 200 => "Area is between 100 and 200", | ||
+ | _ => "" | ||
+ | }; | ||
+ | | ||
+ | } | ||
+ | |||
+ | public static class Extensions | ||
+ | { | ||
+ | public static bool IsLetter(this char c) => | ||
+ | c is >= ' | ||
+ | } | ||
+ | |||
+ | public abstract class Shape | ||
+ | { | ||
+ | public abstract double Area { get; } | ||
+ | } | ||
+ | |||
+ | public class Rectangle : Shape, ISquare | ||
+ | { | ||
+ | public Rectangle(int height, int width) | ||
+ | { | ||
+ | Height = height; | ||
+ | Width = width; | ||
+ | } | ||
+ | | ||
+ | public override double Area => Height * Width; | ||
+ | | ||
+ | public int Height { get; set; } | ||
+ | public int Width { get; set; } | ||
+ | } | ||
+ | |||
+ | public class Circle : Shape | ||
+ | { | ||
+ | private const double PI = Math.PI; | ||
+ | | ||
+ | | ||
+ | public Circle(int diameter) | ||
+ | { | ||
+ | Diameter = diameter; | ||
+ | } | ||
+ | |||
+ | public int Diameter { get; set; } | ||
+ | public int Radius => Diameter / 2; | ||
+ | |||
+ | public override double Area => PI * Radius * Radius; | ||
+ | } | ||
+ | |||
+ | public interface ISquare | ||
+ | { | ||
+ | int Height { get; set; } | ||
+ | int Width { get; set; } | ||
+ | } | ||
+ | |||
+ | static decimal GetGroupTicketPriceDiscount(int groupSize, DateTime visitDate) | ||
+ | => (groupSize, visitDate.DayOfWeek) switch | ||
+ | { | ||
+ | (<= 0, _) => throw new ArgumentException(" | ||
+ | (_, DayOfWeek.Saturday or DayOfWeek.Sunday) => 0.0m, | ||
+ | (>= 5 and < 10, DayOfWeek.Monday) => 20.0m, | ||
+ | (>= 10, DayOfWeek.Monday) => 30.0m, | ||
+ | (>= 5 and < 10, _) => 12.0m, | ||
+ | (>= 10, _) => 15.0m, | ||
+ | _ => 0.0m | ||
+ | }; | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
====== Reactive ====== | ====== Reactive ====== |
developpement/dotnet/csharp/introduction.1696510961.txt.gz · Dernière modification : 2023/10/05 15:02 de sgariepy