Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# 4.0 Anonymous Functions

How do I do the following shown in Javascript in C# 4.0:

var output = doSomething(variable, function() {
    // Anonymous function code
});

I'm sure I've seen this somewhere before but I cannot find any examples.

like image 252
GateKiller Avatar asked May 10 '26 09:05

GateKiller


1 Answers

Using a lambda expression (parameterless, therefore empty parentheses), it is very simple:

var output = doSomething(variable, () => {
    // Anonymous function code
});

In C# 2.0, the syntax was a bit longer:

SomeType output = doSomething(variable, delegate {
    // Anonymous function code
});
like image 141
Mormegil Avatar answered May 14 '26 20:05

Mormegil