(Sep 28, 2015 01:15 AM)xadamxk Wrote: From what I understand, it's similar to the function operator?
Can you give a little more to exclaim it, like a basic example?
I've seen a few of them in samples of code, but I had no idea what it was so I didn't use it.
One of his examples is pretty easy to follow:
Code
public void Main()
{
var foo = 1337;
Func<int, int> f = x => x + 3;
}
So, in that code he is declaring a function f that takes a variable x. The declaration of Func<int, int> is defining the input and output types (int for an input, int for an output) and the lambda expression is basically defining the actual return value, so it is simply x + 3. (Using the x we declared earlier.)
Now, you can easily call this function by simply writing:
Code
public int AddThree(int valueToAdd)
{
Func<int, int> f = x => x + 3;
return f(valueToAdd); // if valueToAdd is 7, then this returns 10
}
Here is another quick example:
Code
static void Main(string[] args)
{
Func<String, String> Wrap = (word => "\"" + word + "\"");
String message = "Hello world!";
Console.WriteLine(Wrap(message));
Console.Read();
}
So all this does is take a string and put quotations around it.
(Hello world! becomes "Hello world!")
Lambdas are awesome! 👍