Advertisement
karlakmkj

Lambda Expressions

Nov 19th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1. using System;
  2.  
  3. namespace AlternateExpressions
  4. {
  5.   class Program
  6.   {
  7.     static void Main(string[] args)
  8.     {
  9.       string[] spaceRocks = {"meteoroid", "meteor", "meteorite"};
  10.      
  11.       //use Lambda on the second argument
  12.       //bool makesContact = Array.Exists(spaceRocks, (string s) => s == "meteorite");
  13.       //for an even more shorter form of Lambda - remove parameter type and parentheses
  14.       bool makesContact = Array.Exists(spaceRocks, s => s == "meteorite");
  15.      
  16.       if (makesContact)
  17.       {
  18.         Console.WriteLine("At least one space rock has reached the Earth's surface!");
  19.       }
  20.     }
  21.    
  22.     //Method won't be used since it is shrink to lambda expression in Main() method
  23.     static bool HitGround(string s)
  24.     {
  25.       return s == "meteorite";
  26.     }
  27.   }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement