Friday, January 13, 2017

Assigning lambda expression to a variable

In the spirit of writing terse code, I was pleasantly surprised that you can assign a lambda expression to a local variable. A quick sample:
void Main()
{
 Func<string, string> sayHello = ((input) => { return input + "!!"; });
 System.Diagnostics.Debug.WriteLine(sayHello("hello world"));
}

Results:
hello world!!

in Func<string, string>, the first string specifies the input parameter's data type, while the last string is for the return type. If you have more than one input parameter, you'd have multiple parameters specified like this <string, string, stringstring> and the last one is always for the return type of the function.

1 comment:

Anonymous said...

The King Casino | The King Casino
At The 제왕카지노 King Casino, you will get a fantastic choice of over 500 exciting online slot machines and 더킹카지노 some table games from the leading software providers. 바카라사이트 The