-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: Jayant_Magic
Message 2 in Discussion
Hi, Answer to first problem as: Delegates are function pointers in C# that are
managed and type safe and can refer to one or more methods that have identical
signatures. Delegates in C# are reference types. They are type safe, managed
function pointers in C# that can be used to invoke a method that the delegate
refers to. The signature of the delegate should be the same as the signature of
the method to which it refers. According to MSDN, "A delegate in C# is similar
to a function pointer in C or C++. Using a delegate allows the programmer to
encapsulate a reference to a method inside a delegate object. The delegate
object can then be passed to code which can call the referenced method, without
having to know at compile time which method will be invoked. Unlike function
pointers in C or C++, delegates are object-oriented, type-safe, and secure."
C# provides support for Delegates through the class called Delegate in the
System namespace. Delegates are of two types. · Single-cast delegates
· Multi-cast delegates A Single-cast delegate is one that can refer to
a single method whereas a Multi-cast delegate can refer to and eventually fire
off multiple methods that have the same signature. The signature of a delegate
type comprises are the following. · The name of the delegate ·
The arguments that the delegate would accept as parameters · The return
type of the delegate A delegate is either public or internal if no specifier is
included in its signature. Further, you should instantiate a delegate prior to
using the same. The following is an example of how a delegate is declared. 1)
Declaring a delegate public delegate void TestDelegate(string message);The
return type of the delegate shown in the above example is "void" and it accepts
a string argument. Note that the keyword "delegate" identifies the above
declaration as a delegate to a method. This delegate can refer to and
eventually invoke a method that can accept a string argument and has a return
type of void, i.e., it does not return any value. You can use a delegate to
make it refer to and invoke a method that has identical signature as the
delegate only. Even if you are using multi-cast delegates, remember that you
can use your delegate to refer to and then fire off multiple methods that have
identical signatures only. A delegate should always be instantiated before it
is used. The following statement in Listing 2 shows how you can instantiate a
delegate.
2) Instantiating a delegate TestDelegate t = new TestDelegate(Display); Example
of single cast delegate: using System;
public delegate void TestDelegate(string message); //Declare the delegate
class Test
{
public static void Display(string message)
{
Console.WriteLine("The string entered is : " + message);
}
static void Main()
{
TestDelegate t = new TestDelegate(Display); //Instantiate the delegate
Console.WriteLine("Please enter a string");
string message = Console.ReadLine();
t(message); //Invoke the delegate
Console.ReadLine();
}
}
Example of multi cast delegate:
using System;
public delegate void TestDelegate();
class Test
{
public static void Display1()
{
Console.WriteLine("This is the first method");
}
public static void Display2()
{
Console.WriteLine("This is the second method");
}
static void Main()
{
TestDelegate t1 = new TestDelegate(Display1);
TestDelegate t2 = new TestDelegate(Display2);
t1 = t1 + t2; // Make t1 a multi-cast delegate
t1(); //Invoke delegate
Console.ReadLine();
}
}
For second problem the disadvantages if you dont use delegates are: 1)dynamic
binding can be done by using delegate because we can call more than one methods
at a time dynamically by calling the delegate in which the methods is defined.
2)Clr provide the faciltity to register a call back function with with contol
and this control call this call back function when particular event occure. 3)
It enables event handling to be handled by objects other than the ones that
generate the events (or their containers). This allows a clean separation
between a component's designand its use. 4) It performs muchbetter in
applications where many events are generated. This performance improvementis
due to the fact that the event-delegation model does not have to repeatedly
processunhandled events, as is the case of the event-inheritance model.
For more refrence you can visit:
http://msdn2.microsoft.com/en-us/library/Aa288459(VS.71).aspx
http://aspalliance.com/1228_Working_with_Delegates_in_C.all Hope this Helps.
Regards
Jayant
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member
Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you
received this message by mistake, please click the "Remove" link below. On the
pre-addressed e-mail message that opens, simply click "Send". Your e-mail
address will be deleted from this group's mailing list.
mailto:[EMAIL PROTECTED]