Assign a variable to the result of a dynamic function call in C #

In JavaScript, I can assign a value to a variable by dynamically creating a function. For instance,

var name = (function () { name="bob"; return name; }());

I am sure that with C # 4.0 the same type is possible. Can someone show me the syntax of how the same line will look in C #?

Also, if you could run through my memory of what the right term is for creating this type of dynamic function, that would be really appreciated!

Thank you for your help!

PS: This question was probably asked before, but since I was unclear in the nomenclature, perhaps I missed it. If this happens, I apologize!

+5
source share
2 answers

:

Func<string> anonymousFunction = () => { string name = "bob"; return name; };
string myName = anonymousFunction();

- , # 3.0 . , :

Func<string, string> makeUppercase = x => x.ToUpper();
string upperCase = makeUppercase("lowercase");

, , . , return, , return.

LINQ, , :

var numbers = new List<int>() { 1, 2, 3, 4 };
var divisibleByTwo = numbers.Where(num => num % 2 == 0);

, #. :

string output = (x => x.ToUpper())("lowercase");

" ". .

+6