Hi all,
I was wondering if someone could explain what Func<int, string>
is and how it is used with some clear examples.
Thanks in advance
Hi all,
I was wondering if someone could explain what Func<int, string>
is and how it is used with some clear examples.
Thanks in advance
It is a delegate that takes one int
as a parameter and returns a value of type string
.
Here is an example of its usage:
using System;
class Program
{
static void Main()
{
Func<Int32, String> func = bar;
// now I have a delegate which
// I can invoke or pass to other
// methods.
func(1);
}
static String bar(Int32 value)
{
return value.ToString();
}
}
MSDN is your friend:
http://msdn.microsoft.com/en-us/library/bb549151.aspx
They have good examples on how to use it.
Edit: I am not trying to post this as a RTFM. Sometimes I find the easiest explanation for something is to look at the MSDN article. - JH
Are you familiar with delegates in general? I have a page about delegates and events which may help if not, although it's more geared towards explaining the differences between the two.
Func<T, TResult>
is just a generic delegate - work out what it means in any particular situation by replacing the type parameters (T
and TResult
) with the corresponding type arguments (int
and string
) in the declaration. I've also renamed it to avoid confusion:
string ExpandedFunc(int x)
In other words, Func<int, string>
is a delegate which represents a function taking an int
argument and returning a string
.
Func<T, TResult>
is often used in LINQ, both for projections and predicates (in the latter case, TResult
is always bool
). For example, you could use a Func<int, string>
to project a sequence of integers into a sequence of strings. Lambda expressions are usually used in LINQ to create the relevant delegates:
Func<int, string> projection = x => "Value=" + x;
int[] values = { 3, 7, 10 };
var strings = values.Select(projection);
foreach (string s in strings)
{
Console.WriteLine(s);
}
Result:
Value=3
Value=7
Value=10
A Func<int, string>
eats ints and returns strings. So, what eats ints and returns strings? How about this ...
public string IntAsString( int i )
{
return i.ToString();
}
There, I just made up a function that eats ints and returns strings. How would I use it?
var lst = new List<int>() { 1, 2, 3, 4, 5 };
string str = String.Empty;
foreach( int i in lst )
{
str += IntAsString(i);
}
// str will be "12345"
Not very sexy, I know, but that's the simple idea that a lot of tricks are based upon. Now, let's use a Func instead.
Func<int, string> fnc = IntAsString;
foreach (int i in lst)
{
str += fnc(i);
}
// str will be "1234512345" assuming we have same str as before
Instead of calling IntAsString on each member, I created a reference to it called fnc (these references to methods are called delegates) and used that instead. (Remember fnc eats ints and returns strings).
This example is not very sexy, but a ton of the clever stuff you will see is based on the simple idea of functions, delegates and extension methods.
One of the best primers on this stuff I've seen is here. He's got a lot more real examples. :)