Geeks With Blogs
Tom Stickel Thoughts on Software Engineering

Func<int, int, int> myfunction = (x,y) => x + y;
This statement consists of three sections.
  1. A declaration: Func<int, int, int> myfunction
  2. An equals operator: =
  3. A lambda expression: (x,y) => x + y;

The variable myfunction points at raw executable code that knows how to add two numbers. The lambda expression shown in step three above is a short hand way of writing the following method:

public int myfunction(int x, int y)
{
  return x + y;
}

One can call either the method shown above, or the lambda expression like this:

int finalnumber = myfunction(4, 6);

After the function is called, the variable finalnumber will be set equal to 4 + 6, which is 10.

The delegate type Func shown above in the declaration found in step one is declared for us in the System namespace:

public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
Posted on Sunday, January 29, 2012 12:36 AM | Back to top


Comments on this post: Func example

# re: Func example
Requesting Gravatar...
Now I have clear the sections of a Func statement, until now I thought that the Lambda expression was all the Func statement, not just a part.
Left by Yelinna on Jan 31, 2012 11:03 AM

Your comment:
 (will show your gravatar)


Copyright © TomStickel | Powered by: GeeksWithBlogs.net