Outils pour utilisateurs

Outils du site


developpement:dotnet:csharp:introduction

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
developpement:dotnet:csharp:introduction [2023/10/04 21:43] – [Task Parallel Library (TPL)] sgariepydeveloppement:dotnet:csharp:introduction [2023/10/06 05:06] (Version actuelle) – [Reactive] sgariepy
Ligne 1410: Ligne 1410:
 } }
 </code> </code>
 +
 +===== TaskCompletionSource =====
 +
 +<code csharp>
 +void Main()
 +{
 +    TaskCompletionSource<Product> taskCompletionSource = new TaskCompletionSource<Product>();
 +    Task<Product> lazyTask = taskCompletionSource.Task;
 +    
 +    Task.Factory.StartNew(() => {
 +        Thread.Sleep(2000);
 +        taskCompletionSource.SetResult(new Product { Id = 1, Name = "Some name" });
 +    });
 +    
 +    Task.Factory.StartNew(() =>
 +    {
 +        if (Console.ReadLine() == "x")
 +        {
 +            Product result = lazyTask.Result;
 +            Console.WriteLine("Result is {0}", result.Name);
 +        }
 +    });
 +    
 +    Thread.Sleep(5000);
 +}
 +
 +class Product
 +{
 +    public int Id { get; set; }
 +    public string Name { get; set; }
 +}
 +
 +</code>
 +
 +===== PLINQ =====
 +
 +Parallel LINQ:
 +
 +  * Automates parallelization
 +  * Considéré déclaratif plutôt qu'impératif
 +  * Opérateurs qui font en sorte que ce n'est pas parallélisé:
 +    * Take, Select, SelectMany, Skip, TakeWhile, SkipWhile, ElementAt
 +  * Anomalies
 +    * Join, GroupBy, GroupJoin, Distinct, Union, Intersect, Except
 +  * Force parallelism:
 +    * .AsParallel().withExecutionMode(ParallelExecution.ForceParallelism)
 +
 +
 +<code csharp>
 +void Main()
 +{
 +    var list = Enumerable.Range(1, 100000);
 +    var primeNumbers = list
 +                        .AsParallel()
 +                        .Where(IsPrime);
 +    Console.WriteLine("{0} prime numbers", primeNumbers.Count());
 +}
 +
 +bool IsPrime(int x)
 +{
 +    if (x == 1) return false;
 +    if (x == 2) return true;
 +    if (x % 2 == 0) return false;
 +    var boundary = (int)Math.Floor(Math.Sqrt(x));
 +
 +    for (int i = 3; i <= boundary; i += 2)
 +    {
 +        if (x % i == 0)
 +        {
 +            return false;
 +        }
 +    }
 +    return true;    
 +}
 +</code>
 +
 +==== Degree of Parallelism ====
 +
 +<code csharp>
 +void Main()
 +{
 +    List<string> websites = new List<string>();
 +    websites.Add("apple.com");
 +    websites.Add("google.com");
 +    websites.Add("microsoft.com");
 +    
 +    List<PingReply> responses = websites
 +                                    .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);
 +}
 +</code>
 +
 +
 +
  
 ====== Thread Marshalling ====== ====== Thread Marshalling ======
Ligne 1415: Ligne 1524:
   * [[http://bigballofmud.wordpress.com/2009/03/21/thread-marshalling-part-1-creating-a-thread-in-windows-forms/|Thread Marshalling]]   * [[http://bigballofmud.wordpress.com/2009/03/21/thread-marshalling-part-1-creating-a-thread-in-windows-forms/|Thread Marshalling]]
   * [[http://www.albahari.com/threading/|Threading (Joseph Albahari)]]   * [[http://www.albahari.com/threading/|Threading (Joseph Albahari)]]
 +
 +
 +====== Pattern Matching ======
 +
 +<code csharp>
 +void Main()
 +{
 +    var circle = new Circle(5);
 +    var circleRadius100 = new Circle(250);
 +    var rectangle = new Rectangle(420, 1337);
 +    var square = new Rectangle(70, 70);
 +
 +    var shapes = new List<Shape> { circle, circleRadius100, rectangle, square };
 +
 +    var randomShape = shapes[new Random().Next(shapes.Count)];
 +
 +    CSharp6Feature(randomShape);
 +    CSharp7Feature(randomShape);
 +    CSharp8Feature(randomShape);
 +    CSharp9Feature(randomShape);
 +}
 +
 +private void CSharp6Feature(Shape shape)
 +{
 +    Console.WriteLine("=== C# 6 Pattern matching features ===");
 +    if (shape is Circle) // 'is' operator
 +    {
 +        var circle = (Circle)shape;
 +        Console.WriteLine($"Circle with radius {circle.Radius}");
 +    }
 +    else
 +    {
 +        Console.WriteLine($"Shape is something else");
 +    }
 +}
 +
 +private void CSharp7Feature(Shape shape)
 +{
 +    Console.WriteLine("=== C# 7 Pattern matching features ===");
 +
 +    if (shape is Circle circle)  // implicit casting
 +    {
 +        Console.WriteLine($"Circle with radius {circle.Radius}");
 +    }
 +    else
 +    {
 +        Console.WriteLine($"Shape is something else");
 +    }
 +    
 +    // using switch
 +    
 +    switch (shape)
 +    {
 +        case Circle c:
 +            Console.WriteLine($"Circle with radius {c.Radius}");
 +            break;
 +        case Rectangle r when r.Height == r.Width:
 +            Console.WriteLine($"This is a square");
 +            break;
 +        default:
 +            Console.WriteLine($"Shape is something else");
 +            break;
 +    }
 +}
 +
 +private void CSharp8Feature(Shape shape)
 +{
 +    Console.WriteLine("=== C# 8 Pattern matching features ===");
 +
 +    if (shape is Circle { Radius: 10 })
 +    {
 +        Console.WriteLine($"Circle with radius of 10");
 +    }
 +    
 +    var shapeDetails = shape switch
 +    {
 +        Circle => "This is a circle", // we can drop the 'cir' is not used
 +        Rectangle rec when rec.Height == rec.Width => "This is a square",
 +        _ => "Shape is something else"
 +    };
 +}
 +
 +private void CSharp9Feature(Shape shape)
 +{
 +    Console.WriteLine("=== C# 9 Pattern matching features ===");
 +
 +    if (shape is not Rectangle) // 'not' added
 +    {
 +        Console.WriteLine($"This is not a rectangle");
 +    }
 +
 +    if (shape is Circle { Radius: > 100 and < 200, Area: >= 1000 })
 +    {
 +        Console.WriteLine($"Circle with radius greater than 100");
 +    }
 +
 +
 +    // that can be used like so: if (shape is not null) {...}
 +
 +    var shapeDetails = shape switch
 +    {
 +        Circle => "This is a circle", // we can drop the 'cir' is not used
 +        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 >= 'a' and <= 'z' or >= 'A' and <= 'Z';
 +}
 +
 +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("Group size must be positive"),
 +        (_, 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
 +    };
 +
 +</code>
 +
 +
 +
  
 ====== Reactive ====== ====== Reactive ======
developpement/dotnet/csharp/introduction.1696448639.txt.gz · Dernière modification : 2023/10/04 21:43 de sgariepy