Introduction:
A
delegate is a class that can hold a reference to a method. Unlike other
classes, a delegate class has a signature, and it can hold references only to
methods that match its signature. A delegate is thus equivalent to a type-safe
function pointer or a callback
The signature of a single cast delegate is shown below:
delegate result-type identifier
([parameters]);
Where:
- result-type:
The result type, which matches the return type of the function.
- identifier:
The delegate name.
- Parameters:
The Parameters, which the function takes.
Examples:
Following declaration define delegate name Add
Pointer which will encapsulate any method that takes two objects as parameter
and return an int.
When to Use:
· I explained with following simple scenario I have
class Called ClsMaths. it basically
contain four method Add, Substract, Division and Multiplication. If suppose I have
to call four method in Cls Maths class.
with following example we can get it.
· Instead creating object every time new method
will be added we can create pointer and pointer with matching argument point to
ClsMaths method. So we can avoid heavy coupling between ClsMaths and UI code.
· For your understanding I have created simple
application. With help of operation we can get result either it would be
addition, subtract, Multiplication or Division.
· I have Create ClsMaths Class
· For Presentation Layer code look like this.
· If Suppose I pass one value in Operation Textbox it will call method Add, two for Subtraction like that.
Out Put:
No comments:
Post a Comment