Advertisement
fcamuso

C# 9, target-typed, lambda

Jan 23rd, 2022
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.07 KB | None | 0 0
  1. using System;
  2.  
  3. namespace TargetType
  4. {
  5.     internal class Program
  6.     {
  7.         class Classe1
  8.         {
  9.             public Classe1(int n, string s) { }
  10.  
  11.             public void unMetodo(Action<string> lambda)
  12.             {
  13.                 lambda("ciao");
  14.             }
  15.         }
  16.  
  17.         class Classe2
  18.         {
  19.             Classe1 objClasse1 = new(3, "ciao");
  20.             public Classe2(Classe1 obj) { }
  21.         }
  22.  
  23.         class Madre { }
  24.         class Figlia1 : Madre { }
  25.         class Figlia2 : Madre { }
  26.  
  27.  
  28.         static void Main(string[] args)
  29.         {
  30.            Random rnd = new Random();
  31.            Madre m = rnd.Next(1,2)==0 ? new Figlia1() : new Figlia2 ();
  32.  
  33.             // targed typed new
  34.             Classe1 oggetto = new(5, "ciao");
  35.  
  36.             Classe2 oggettoC2 = new(new(3, "Ciao"));
  37.  
  38.             //oggetto.unMetodo((string s) => Console.WriteLine( oggettoC2.ToString()));
  39.             const int i = 999;
  40.             oggetto.unMetodo(static (string s) => Console.WriteLine(i)); ;
  41.             Console.WriteLine(i);
  42.  
  43.         }
  44.     }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement