What Are C# Lambda Expressions? How Are They Used?

c# Lambda

What Are C# Lambda Expressions? How Are They Used?

 Lambda expressions are simplified and anonymous functions. Lambda expressions are the adaptation of Lambda Calculus, found in Mathematics and Computer Science, to C# and Visual Basic.

I should say that for simple operations, especially for local functions, it has a very life-saving structure. When used with Unity, it makes the software part even easier in many plugins like DoTween, or rather, it makes your code easier to read.

Example
i => i * i
There is such a usage.

If we explain this example a bit, the "=>" symbol here is the lambda sign. The left side of the lambda sign represents the parameter, while the right side represents the expression.

If we were to write the lambda expression/function here as a normal function, it would be as follows.

Example
int square (int i){
 return i * i;
}

Here we wrote a non-anonymous function and did the process we did in the lambda expression in a much longer way.

We said that lambda expressions are anonymous; if we were to write the function above anonymously, how would we do it? Let's see.

delegate(int i){
return i * i;
}

Here, since the function's return type is not known at the beginning, it is an anonymous function. If we delete the "delegate" part here, we get a lambda function.

(int i){
return i * i;
}

Now we have actually created our anonymous lambda function in a simple way, but as we said at the beginning of the article, they are simplified and anonymous. Now let's simplify this lambda form.

Here, the curly brackets will turn into a lambda expression and our function will take the following form.

(int i ) => return i * i;

Now here, as we mentioned above, the left is the parameter and the right is the expression. Since i*i is already an expression, we can now remove the ";" and "return" words, so our new function will be as follows.

(int i) => i * i 

Here, you can enter the input value optionally or not, it is entirely up to you. If you do not want to enter it, you can use it as follows.

i => i * i

Above, we talked about there being a parameter on the left, but not every lambda function/expression requires a parameter and you can also use it as in the following example.

() => function()

Just as there may be no parameter part, more than one parameter value can also be taken as in the following example.

(a,b) => a > b

Yes, friends, I think I have given enough information about Lambda expressions and I want to give a few examples.

Examples

You can check my article about DoTween in Unity here. In that post, I showed the use of Lambda expression to create a Tween simply.

protected void OzelDonmeAnimasyonu(GameObject obje, Vector3 donmeAcisi, float animasyonTamamlanmaSuresi, Ease animasyonYumusatma, RotateMode donmeSekli, System.Action<int> IslemSonucu)
    {
        obje.transform.DORotate(donmeAcisi, animasyonTamamlanmaSuresi, donmeSekli).SetEase(animasyonYumusatma).OnComplete(() => IslemSonucu(0));
    }
    
    void IslemSonucu(int i){
    if(i == 0){
    Debug.Log("Operation Completed");    
    }
    }
    
    

As you can see in the code above, you can use it like this.