As MSDN said, Func<>
is itself pre-defined Delegate
. For the first time, I confused about this stuff. After the experimental, my understanding was quite clearer. Normally, in C#, we can see
Type
as a pointer to Instance
.
The same concept is applied to
Delegate
as a pointer to Method
The difference between these to things is Delegate
do not possess the concept of OOP, for example, Inheritance
. To make this things clearer, I did the experimental with
public delegate string CustomDelegate(string a);
// Func<> is a delegate itself, BUILD-IN delegate
//==========
// Short Version Anonymous Function
Func<string, string> fShort = a => "ttt";
//----------
// Long Version Anonymous Function
Func<string, string> fLong = delegate(string a)
{
return "ttt";
};
//----------
MyDelegate customDlg;
Func<string, string> fAssign;
// if we do the thing like this we get the compilation error!!
// because fAssign is not the same KIND as customDlg
//fAssign = customDlg;
Many build-in methods in framework (for example, LINQ), receive the parameter of Func<>
delegate. The thing we can do with this method is
Declare
the delegate of Func<>
type and pass it to the function rather than Define
the custom delegate.
For example, from the code above I add more code
string[] strList = { "abc", "abcd", "abcdef" };
strList.Select(fAssign); // is valid
//strList.Select(customDlg); // Compilation Error!!