Why is lambda expression used in DOTween?

Why not? ;)

I don't know that API too much but it seems like it is simply expecting something like

OnComplete(TweenCallback callback)

where TweenCallback from your usage basically seems to equal the c# built-in Action delegate and basically simply a parameter less void

public delegate void TweenCallback();

so whether you pass in this callback as a lambda like

animation.OnComplete(() => DestroyOnCompleted(children));

or anonymous method using the delegate operator like

animation.OnComplete(delegate { DestroyOnCompleted(children); });

or using a method

animation.OnComplete(OnCompletedAnimation);
        
...

private void OnCompletedAnimation()
{
    DestroyOnCompleted(children);
}

is basically equivalent.

The main difference between the first two and the last one is: Where does children come from?

The lambda and delegate way allows you to pass in children from the current scope variables without having to store it in any field!


If you look at the documentation of DotTween, you see that row:

enter image description here

Now looking at the Source Code of DotTween, you can see the definition of TweenCallback:

public delegate void TweenCallback();

So the question now is, what is a delegate void in c#?

A delegate in c# is basically an object that "represent" a function. But functions are not all the same, they can have parameters in input and return something (or return void). To understand what kind of function does a delegate represent, try to just remove the keyword delegate.

For example, the TweenCallback without the keyboard delegate is:

public void TweenCaalback()

So the delegate represent a void function that has no parameters in input! (And it is Public).

What does it means represent a function?

It means that this is valid code:

void DoNothing()
{

}

TweenCallback x = DoNothing;
x();

So you can "assign functions" to a delegate that has the same function signature. In this case, TweenCallback is a delegate void (), so you can assign to it a void() function.

What is a lambda?

A lambda is an expression of that style: (string name, int age) => { return 3 };

you can read that as "string name and int age go in return 3"

That's a more concise way to describe that function:

int AnonymousFunction (string name, int age) {}

The main difference is that lambdas do not have any name. If you have not any parameter in input the lambda become like this:

() => {return 3;}

If you have only one statement inside the {} you are allowed to write it more shortly as

() => 3;

Final step

Is this valid code?

void DoNothing()
{

}

TweenCallback x = () => DoNothing();

Yes it is! Tween callback is expects a void () function.

() => DoNothing(); Is a lambda (un-named function) that takes nothing in input and calls some other function. It's the shorter version of () => {DoNothing();} that you have to think as void () {DoNothing();}

So when writing

animation.OnComplete(() => DestroyOnCompleted(children));

You are just passing a void () function to OnComplete Method, that makes sense because TweenCallback is a void () delegate.

Notes

As you can see, functions and lambdas expression can be converted implicitly to delegates. But you have to understand that they are all different things, and in more advanced coding scenarios that distinction is not just pure theory.