but it is difficult to me understand.please give some simple scenario so why we need it
if not used delegate what will be the problem....
A delegateis a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke
(or call) the method through the delegate instance.
The main benefit of delegate is that you can assign or pass methods/properties as a parameter. Delegate can be understood as a kind of interfaces, and specific methods to achieve this interface, only need to be concerned at the development stage can call
the delegate definition, regardless of how or where it achieved.
It's useful in GUI programming because your whole program could be waiting for the user to do something and you have no way of knowing what they will do first. Using delegates allows the functionality of your program to be connected to the UI in such a way
that the user can do things in any way they want.
You could refer to the following articles, they provide us some detailed explanation.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
usingSystem;delegateintNumberChanger(int n);namespaceDelegateAppl{classTestDelegate{staticint num =10;publicstaticintAddNum(int p){
num += p;return num;}publicstaticintMultNum(int q){
num *= q;return num;}publicstaticint getNum(){return num;}staticvoidMain(string[] args){//create delegate instancesNumberChanger nc1 =newNumberChanger(AddNum);NumberChanger nc2 =newNumberChanger(MultNum);//calling the methods using the delegate objects
nc1(25);Console.WriteLine("Value of Num: {0}", getNum());
nc2(5);Console.WriteLine("Value of Num: {0}", getNum());Console.ReadKey();}}}
below are some point
ref
Delegates can be chained together; for example, multiple methods can be called on a single event. how is it
in this example what is advantage of using delegate.
they told like The reference can be changed at runtime. but i am confusing on it
>>The reference can be changed at runtime. but i am confusing on it
siddangoud
The reference can be changed at runtime. but i am confusing on it
May be multiple objects can be assigned/remove to one delegate instance by using the +/- operator.
You can start a new thread if you have a new question.
Best Regards,
Yong Lu
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
5 Points
53 Posts
why we need delgate in C#
May 08, 2018 10:33 PM|siddangoud|LINK
hi all
i am learning the delegate m i am referring this link
https://buildplease.com/pages/why-delegates/
but it is difficult to me understand.please give some simple scenario so why we need it
if not used delegate what will be the problem....
thanks and reagrds
siddu
Star
11464 Points
2439 Posts
Re: why we need delgate in C#
May 09, 2018 01:54 AM|Yohann Lu|LINK
Hi siddangoud,
A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.
The main benefit of delegate is that you can assign or pass methods/properties as a parameter. Delegate can be understood as a kind of interfaces, and specific methods to achieve this interface, only need to be concerned at the development stage can call the delegate definition, regardless of how or where it achieved.
It's useful in GUI programming because your whole program could be waiting for the user to do something and you have no way of knowing what they will do first. Using delegates allows the functionality of your program to be connected to the UI in such a way that the user can do things in any way they want.
You could refer to the following articles, they provide us some detailed explanation.
6 important uses of Delegates and Events
C# - Delegates 101 - A Practical Example:
Best Regards,
Yong Lu
Member
5 Points
53 Posts
Re: why we need delgate in C#
May 09, 2018 03:10 AM|siddangoud|LINK
Hi
thanks for reply...
below code u can ref
Star
11464 Points
2439 Posts
Re: why we need delgate in C#
May 10, 2018 02:52 AM|Yohann Lu|LINK
Hi siddangoud,
You can refer the following sample.
How to: Combine Delegates (Multicast Delegates)(C# Programming Guide):
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/how-to-combine-delegates-multicast-delegates
>>The reference can be changed at runtime. but i am confusing on it
May be multiple objects can be assigned/remove to one delegate instance by using the +/- operator.
You can start a new thread if you have a new question.
Best Regards,
Yong Lu