c# func delegate |
GENERIC DELEGATE
“The entire predefined delegates are
known as generic delegate. Actually in our base class library we found some
predefined delegate by the programmer and we just use them in our
program/project to fulfill our requirements is called generic delegate.”
The advantage of generic delegate
is that we need not to defined an explicit delegate for the methods separately,
in place of this we use the generic delegate at the time of creating instance we
just use the keyword and make it possible example will clear the confusions.
There are
three basic predefined or generic delegate in our system.io is
FUNC DELEGATE IN C#
Func
delegate use when we have a method that returns value, simple it is used for the
value returning methods. Actually ‘Func’ is
the keyword to call the delegates. The Func delegate contain 17 overload
delegate in it for which in sequence all delegates accept different types
of parameter but return one result/value
that’s are below in the table
Sr.no
|
Delegate
|
Parameter list
|
Return value
number
|
1
|
1st
delegate
|
Zero parameter
|
One return type
|
2
|
2nd
delegate
|
1 parameter
|
One return type
|
3
|
3rd
delegate
|
2 parameters
|
One return type
|
4
|
4th
delegate
|
3 parameters
|
One return type
|
5
|
5th
delegate
|
4 parameters
|
One return type
|
6
|
6th
delegate
|
5 parameters
|
One return type
|
7
|
7th
delegate
|
6 parameters
|
One return type
|
8
|
8th
delegate
|
7 parameters
|
One return type
|
9
|
9th
delegate
|
8 parameters
|
One return type
|
10
|
10th
delegate
|
9 parameters
|
One return type
|
11
|
11th
delegate
|
10 parameters
|
One return type
|
12
|
12th
delegate
|
11 parameters
|
One return type
|
13
|
13th
delegate
|
12 parameters
|
One return type
|
14
|
14th
delegate
|
13 parameters
|
One return type
|
15
|
15th
delegate
|
14 parameters
|
One return type
|
16
|
16th
delegate
|
15 parameters
|
One return type
|
17
|
17th
delegate
|
16 parameters
|
One return type
|
Syntax
Func <parameter
list, return type > Obj = <method name>
Func is the keyword and
then parameter list that how many parameter
that have to pass according to programmer return
type we have to explain which type of value should be return either may
be int, double , string or bool etc. Obj is
the object name it may be on the programmer chose he can named any name such as
a , b h ,cd, nor etc. etc. and last the method name in the place of this that method name
come that should bind to this delegate the syntax is same for all three generic
delegate while changing the first keyword from func to action and predicate but
all other steps will be same. Calling will same as delegate calling. See it.
Func
<parameter list only type, return type > Obj = <method name>
It can be short if we use lambda expression on Func delegate
not only Func all the three generic delegate can make essay by using lambda
expression how can we make it just remove the method name and specify the
lambda expression as we did in lambda
expression part like this.
Func
<formal parameter list only type, return type > Obj = (actual parameter
list only name) =>
{
Method implementation;
}
you can see what is formal parameter and actual parameter by
clicking on them.
c# func delegate |
EXAMPLE OF FUNC DELEGATE IN C#
Suppose the
simple problem, in 1st example we make user defined delegate to
execute the program and in 2nd example we will make execution possible
by using Func delegate and also use lambda expression along with Func delegate.
1st Example without Func delegate use
namespace FuncDelegate
{
public delegate double Addnum(int a, double b, float c);
public delegate string SayDelegate(string str);
class Program
{
public static double Add(int a, double b, float c)
{
return a + b + c;
}
public static string Saything(string str)
{
return "hy "+str+" you are
using Visual stdio 2020";
}
static void Main(string[] args)
{
Addnum ad = Add;
double d= ad(12, 23.45, 34.4f);
Console.WriteLine(d);
SayDelegate sd = Saything;
string str = sd("kashif");
Console.WriteLine(str);
Console.ReadKey();
}
}
}
|
2nd example Func delegate use
namespace FuncDelegate
{
class Program
{
static void Main(string[] args)
{
Func< int , double , float , double > ad = (a,b,c)
=>
{
return a + b + c;
};
double d= ad(12, 23.45, 34.4f);
Console.WriteLine(d);
Func<string , string > sd = (str)
=>
{
return "hy " + str + " you are
using Visual stdio 2020";
};
string s = sd("kashif");
Console.WriteLine(s);
Console.ReadKey();
}
}
}
|
action delegate
Predicate delegate
0 comments:
Post a Comment
Please do not enter any spam link in the comment box