what is deleagte?
i new 1 in c#. 1 can explain delegate me? examples
hi amonnn,
thank posting here.
>>what delegate?
a delegate type represents references methods particular parameter list , return type. when instantiate delegate, can associate instance method compatible signature , return type. can invoke (or call) method through delegate instance. delegates used pass methods arguments other methods.
here simple example.
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; namespace @delegate { class program { public delegate int calculate(int value1, int value2); static void main(string[] args) { //creating class contains methods //that assigned delegate objects program p = new program(); //creating delegate objects , assigning appropriate methods //having exact signature of delegate calculate add = new calculate(p.add); calculate sub = new calculate(p.sub); //using delegate objects call assigned methods console.writeline("adding 2 values: " + add(10, 6)); console.writeline("subtracting 2 values: " + sub(10, 4)); console.readkey(); } //a method, assigned delegate objects //having exact signature of delegate public int add(int value1, int value2) { return value1 + value2; } //a method, assigned delegate objects //having exact signature of delegate public int sub(int value1, int value2) { return value1 - value2; } } }
for more details, please refer msdn article.
i hope helpful you.
if have issue, please feel free contact us.
best regards,
wendy
msdn community support
please remember click "mark answer" responses resolved issue, , click "unmark answer" if not. can beneficial other community members reading thread. if have compliments or complaints msdn support, feel free contact msdnfsf@microsoft.com.
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment