what is the use of Delegates and why it is use of delegates main reason plz tell me real time example
thanks for advance
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
sureshkumar spPosted Nov 5, 2013, 3:58 AM
Best understand of Delegates is can like, in OOPS methodology we can use the value of the member variables in bi-direction right, only can call the member function and get the result. If you want to define a method definition from out side the object or allow the people to change your method definition, delegates support for that.
By delegates we can pass method as parameter to a function or to any instance.
Keyword : delegate
: [Access Modifier] delegate return type DelegateName( [arguments])
Find the attachment to for more your reference.
Maroof RainiPosted Nov 4, 2013, 10:06 AM
For example we have 2 methods as Square and Cube as below-
int Square(int a) //An original Method
{
return a*a;
}
int Cube(int b) // An original Method
{
return a*a*a;
}
delegate int IAmADelegate(int num) /* A delegate, who says that I can represent some method who takes one integer as parameter and returns an integer*/
protected void Page_Load(object sender, EventArgs e)
{
IAmADelegate IMD += new IAmADelegate(Sqaure); /*I am a delegate who will represent Square method so now you can get your task done from me. (Actually I will point your parameter to square method and the method will return its value)*/
Response.Write(IMD(10).ToString()); // Returns 100
Response.Write("
");
IMD += IAmADelegate(Cube);/* Now I am a delegate who will represent Cube method so now you can get your task done from me. (Actually this time I will point your parameter to cube method to get the cube) --- See my skill here, now I am representing Cube means I can represent any method who takes one integer and returns an integer, this is why I am used happily */
Response.Write(a(10).ToString());// Returns 1000
}
In below example its delegate who actually points the method which needs to be executed on click of the button as end with "Handles button click"
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click