A Func<int, string>
is basically a delegate with the following signature:
public delegate string Func(int I)
Prior to C# 2, The only way to pass a method to a delegate (which is a pointer to a function) was to explicitly create a named method with the same signature as the delegate above, like the following:
public string MyAwsomeFunc(int I)
And that method could be passed to a method containing the delegate as a parameter:
public void SomeFunc(Func callback)
C# 2 introduced Annonymous Methods, which enabled us to create delegate handlers "on the fly", letting the compiler create the named method for us:
// Create a delegate.
delegate string Func(int x);
// Instantiate the delegate using an anonymous method.
Func d = delegate(int k) { /* ... */ };
C# 3 introduced us to Lamba Expressions, which is the preferred way of handling delegates in C#. A lambda expression is an anonymous method that you can use to create a delegate. By using lambda expressions, you can write local functions that can be passed as arguments or returned as the value of function calls:
delegate int del(int i);
del myDelegate = x => x * x // lamba expression syntax
int j = myDelegate(5); //j = 25
What happened in your method is that it accepts a delegate type of Func<int, string>
, and using the lamba expression syntax:
x => Url.Action("Index", new { page = x }) )
x is the input parameter for Func, which is an int and is being assigned to the page
property, and the Url.Action method returns a string, which is what the return type is expected to be. The compiler is doing the work of generating a delegate handler for us, so we dont have to. It would be the same as using the following approach with a named method:
string MyMethod(int I)
{
string result = Url.Action("index", new { page = I });
return result;
}
Then, sending MyMethod as a parameter:
@Html.RecordPager(Model.PagingIngModel, MyMethod(someInt));
Edit
Some reading material:
- Eric Lippert - Annonymous Methods vs Lambda Expressions - Part 1 (Continue reading the rest of the series, there are five parts)
- Annonymous Methods (MSDN)
- Lambda Expressions (MSDN)