Confusion
-
-
please tell me the concept of delegates. I have seen from it from book but didnt get it properly.. what i read is it is a type safe object that can point to another method or multiple method in the application which can be invoked at later time.
What you have read and understood is actually quite correct. A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure. The type of a delegate is defined by the name of the delegate. A delegate encapsulates a method. A delegate object is normally constructed by providing the name of the method the delegate will wrap, or with an anonymous Method. Once a delegate is instantiated, a method call made to the delegate will be passed by the delegate to that method. The parameters passed to the delegate by the caller are passed to the method, and the return value, if any, from the method is returned to the caller by the delegate. This is known as invoking the delegate. An instantiated delegate can be invoked as if it were the wrapped method itself. E.g. In the example below, a delegate (with a string as parameter) is created and then invoked by using the handler. The handler points to the actual method.
public delegate void Del(string message);
// Create a method for a delegate.
public static void DelegateMethod(string message)
{
System.Console.WriteLine(message);
}// Instantiate the delegate.
Del handler = DelegateMethod;// Call the delegate.
handler("Hello World");Note: Text and example taken from http://msdn.microsoft.com/en-us/library/ms173172.aspx[^].
Build your own survey - http://www.factile.net
-
please tell me the concept of delegates. I have seen from it from book but didnt get it properly.. what i read is it is a type safe object that can point to another method or multiple method in the application which can be invoked at later time.
That's about the size of it! A delegate is a variable, but instead of having a value that you can add or subtract, the value is the address of a method that you can call. At it's most basic, a delegate allows you to specify a routine to execute at runtime rather than compile time. This can make code more flexible: a method could print data to a variety of sources without ever needing to know where the data is going, because it just calls the method that the delegate refers to. So as long as all the methods you pass to the delegate operate correctly on the data, you can add a destination without changing the code that does the mechanics of the printing at all.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
-
please tell me the concept of delegates. I have seen from it from book but didnt get it properly.. what i read is it is a type safe object that can point to another method or multiple method in the application which can be invoked at later time.
I am going to dare to respond with an emphasis that is a bit at variance with the two other commentators' remarks here (both of whom I regard, personally, as "CodeProject Bacon, prime cut, extra-lean," and respected mentors). I must, however, on one point, dare to question OG's description of a delegate as a "variable." I don't think that's a helpful way to describe what they are, particularly to those just getting into the concept, even though it's "technically true," on a certain level. I would describe a delegate as a Type which is a declaration of a signature of an encapsulated Method. Where "signature" means a specification of the types of expected output Type and inputs (usual list of parameters) that instances of the delegate will, later, "contain." Numerous instances of the delegate can be created: as long as each new instance of the delegate has assigned to it a method (or an in-line anonymous method is provided, and assigned) whose parameters match the signature of the delegate: you have a "legal" instance of the delegate. Let's look at a trivial example of using the same delegate more than once:
public delegate int trivialDelegate(int int1, int int2);
public static int AddEm(int int1,int int2)
{
return int1 + int2;
}public static int DivEm(int int1,int int2)
{
return int1 / int2;
}public static trivialDelegate lateBoundAdd = AddEm;
public static trivialDelegate lateBoundDiv = DivEm;
// example of in-line anonymous method expressed in lambda form
public static trivialDelegate lateBoundSub = (int1, int2) => int1 - int2;So, now, somewhere in your code at run-time, you can invoke these late-bound methods:
// somewhere in your code ... later
int addResult = lateBoundAdd(100, 200);
int divResult = lateBoundDiv(1000, 333);
int subResult = lateBoundSub(999, 666);So, what's the big deal about "late-binding:" it means you could change the code in the method that is assigned to delegate, at any time, and add other calls to it, do other things with it, at run-time, as long as your new method assigned to the instance name "obeys" the signature of the delegate's required syntax.
The glyphs you are reading now: are place-holders signifying the total absence of a signature.