n4wn4w

C# lambda expression

May 2nd, 2015
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.24 KB | None | 0 0
  1.  
  2. /////////////////////// array.Sort
  3.  var arr = new[]
  4.             {
  5.                 new{X = 3, Y = 5},
  6.                 new{X = 3, Y = 6},
  7.                 new{X = 33, Y = 5},
  8.                 new{X = 34, Y = 55},
  9.                 new{X = 333, Y = 54},
  10.                 new{X = 33, Y = 6},
  11.                 new{X = 13, Y = 5},
  12.                 new{X = 21, Y = 16},
  13.             };
  14.  
  15.  
  16.             Array.Sort(arr,
  17.                 (a, b) => -a.Y.CompareTo(b.Y) == 0 ?
  18.                 a.X.CompareTo(b.X) :
  19.                 -a.Y.CompareTo(b.Y)
  20.                 );
  21.  
  22.             foreach (var item in arr)
  23.             {
  24.                 Console.WriteLine(item);
  25.             }
  26.  
  27.             Console.WriteLine();
  28.             Console.WriteLine();
  29.  
  30.             Array.Sort(arr, (a, b) =>
  31.                   {
  32.                       if (a.Y == b.Y)
  33.                       {
  34.                          return a.X.CompareTo(b.X);
  35.                       }
  36.                       else
  37.                       {
  38.                          return a.Y.CompareTo(b.Y);
  39.                       }
  40.                   }
  41.                   );
  42.  
  43.             foreach (var item in arr)
  44.             {
  45.                 Console.WriteLine(item);
  46.             }
  47.  
  48. ////////////////////// find all vsi4kite chetni chisla  ///////////////////////
  49.  
  50.  
  51.  
  52.  List<int> list = new List<int>() { 1, 2, 3, 4 };
  53.             List<int> evenNumbers = list.FindAll(x => (x % 2) == 0);
  54.             foreach (var num in evenNumbers)
  55.             {
  56.                 Console.Write("{0} ", num);
  57.             }
  58.             Console.WriteLine();
  59.             // 2 4
  60.  
  61.  
  62. ///////////////////////  remove all  po-golemi ot 6   ///////////////////////////////////
  63.  
  64.  
  65.             List<int> list = new List<int>() { 1, 2, 3, 4,5,6,7,8,9,0,12,-5 };
  66.  
  67.             list.RemoveAll(x => x > 6);
  68.             foreach (var num in list)
  69.             {
  70.                 Console.Write("{0} ", num);
  71.             }
  72.             Console.WriteLine();
  73.             // 1 2 3
  74.  
  75.  
  76. /////////////////////// OrderBy  pets po godini moje i po imena  v azbuchen red ////////////////////////
  77.  
  78.  
  79.     class Pet
  80.     {
  81.         public string Name { get; set; }
  82.         public int Age { get; set; }
  83.     }
  84.  
  85.  
  86.         static void Main(string[] args)
  87.         {
  88.  
  89.         var pets = new Pet[]
  90.         {
  91.           new Pet { Name="Sharo", Age=8 },
  92.           new Pet { Name="Rex", Age=4 },
  93.           new Pet { Name="Strela", Age=1 },
  94.           new Pet { Name="Bora", Age=3 }
  95.         };
  96.             var sortedPets = pets.OrderBy(pet => pet.Age);
  97.  
  98.             foreach (Pet pet in sortedPets)
  99.             {
  100.                 Console.WriteLine("{0} -> {1}",
  101.                   pet.Name, pet.Age);
  102.             }
  103.  
  104.  
  105. ////////////// findAll   che moga vutre v lambda expresinite da si otvarqm { } skobi i da si pishe  algoritmi ////////
  106.  
  107.  
  108.             list = new List<int>()
  109.             { 20, 1, 4, 8, 9, 44 };
  110.  
  111.         // Process each argument with code statements
  112.         evenNumbers = list.FindAll((i) =>
  113.             {
  114.                 Console.WriteLine("value of i is: {0}", i);
  115.                 return (i % 2) == 0;
  116.             });
  117.  
  118.         Console.WriteLine("Here are your even numbers:");
  119.         foreach (int even in evenNumbers)
  120.             Console.Write("{0} ", even);
  121.  
  122.  
  123.  
  124.  
  125.  
  126. //////////////////   funk bool   ////////////////////////////////////////////////
  127.  
  128.  Func<bool> boolFunc = () => true;
  129.             Func<int, bool> intFunc = (x) => x < 10;
  130.             if (boolFunc() && intFunc(5))
  131.             {
  132.                 Console.WriteLine("5 < 10");
  133.             }
  134.          
  135.  
  136.  
  137. /////////////////// finAll i davame delegate vsi4ki suvpadashti na 'S' i da gi natrupva vuv lista townsWithS ///////
  138.  
  139.   List<string> towns = new List<string>()
  140.         {
  141.           "Sofia", "Plovdiv", "Varna", "Sopot", "Silistra"
  142.         };
  143.  
  144.             List<string> townsWithS =
  145.               towns.FindAll(delegate(string town)
  146.               {
  147.                   return town.StartsWith("S");
  148.               });
  149.  
  150.             // A short form of the above (with lambda expression)
  151.             //List<string> townsWithS = towns.FindAll((town) => town.StartsWith("S"));
  152.  
  153.             foreach (string town in townsWithS)
  154.             {
  155.                 Console.WriteLine(town);
  156.             }
Advertisement
Add Comment
Please, Sign In to add comment