C# Fundamentals - Lambda Expressions


Lambda Expressions - Introduction

Lambda expressions are how anonymous functions are created. It's a very powerful syntactic sugar that is available in C#.
At syntax level,The lambda operator => divides a lambda expression into two parts. The left side is the input parameter and the right side is the lambda body.
 A lambda expression can be simply viewed as a function or method without name, which looks like method parameter(s) => method body, or method parameter(s) => method return value. The => operator is called lambda operator and reads “go to”.
 In order to get a grasp of how lambdas work we will go through some examples.
Simple Lambda Example
using System;  
using System.Collections.Generic;  
using System.Linq;  
public static class BitShift
{  
        public static void Main()  
        {  
            List<int> list = new List<int>() { 10, 12, 13, 14, 15, 16 };  
            List<int> evenNumbers = list.FindAll(x => (x % 2) == 0);       
            foreach (var num in evenNumbers)  
            {  
                Console.Write("{0} ", num);  
            }  
            Console.WriteLine();  
            Console.Read();        
        }  
}  
10 12 14 16
The preceding example loops through the entire collection of numbers and each element (named x) is checked to determine if the number is a multiple of 2 (using the Boolean expression (x % 2) == 0).
Example 2
using System;  
using System.Collections.Generic;  
using System.Linq;  
class Game
{  
        public string Name { get; set; }  
        public int Rating{ get; set; } 
}    
class BitShift{  
    static void Main()  
    {  
        List<Game> games= new List<Game>() {   
        new Game{ Name = "Pokemon GO", Rating= 3 },  
        new Game{ Name = "Halo", Rating= 5 },  
        new Game{ Name = "Civilization 6", Rating= 4 }  
        };  
        var names = games.Select(x => x.Name);  
        foreach (var name in games)  
        {  
           Console.WriteLine(name);                   
        }  
        Console.Read();  
    }  
};  
Pokemon GO
Halo
Civilization 6
We create a collection, containing data from a certain class. In the example, from the class Game(with properties Name and Rating), we want to get a list that contains all the game names. With the keyword var, we tell the compiler to define the type of the variable depending on the result that we assigned on the right side of the equals sign.
Passing Lambda As Delegate Parameter
using System;
public delegate void JustToPrint(string data);
public class BitShift
{
       public static void Main(string[] args)
        {
            Printer(MatchingWithDelegateFunc, "Param 1");
            Printer((s)=>{Console.WriteLine("Called from anonymous funcion : " + s);}, "Param 2");
        }
        public static void Printer(JustToPrint printFunc, string data)
        {
            printFunc(data);
        }
        public static void MatchingWithDelegateFunc(string data)
        {
            Console.WriteLine("Called from non-anonymous function : " + data);
        }
    }
}
Called from non-anonymous function: Param 1
Called from anonymous function : Param 2
Support Bitshift Programmer by leaving a like on Bitshift Programmer Facebook Page and be updated as soon as there is a new blog post.
If you have any questions that you might have about shaders, C# or Unity development in general don't be shy and leave a message on my facebook page or down in the comments.
For More C# Tutorials, go HERE.
For Unity Tutorials, go HERE.