Vyoms OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Bookmark and Share Rss Feeds

ABC's of Delegate | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Sponsored Ads
Hot Jobs
Fresher Jobs
Experienced Jobs
Government Jobs
Walkin Jobs
Placement Section
Company Profiles
Interview Questions
Placement Papers
Resources @ VYOMS
Companies In India
Consultants In India
Colleges In India
Exams In India
Latest Results
Notifications In India
Call Centers In India
Training Institutes In India
Job Communities In India
Courses In India
Jobs by Keyskills
Jobs by Functional Areas
Learn @ VYOMS
GATE Preparation
GRE Preparation
GMAT Preparation
IAS Preparation
SAP Preparation
Testing Preparation
MBA Preparation
News @ VYOMS
Freshers News
Job Articles
Latest News
India News Network
Interview Ebook
Get 30,000+ Interview Questions & Answers in an eBook.
Interview Success Kit - Get Success in Job Interviews
  • 30,000+ Interview Questions
  • Most Questions Answered
  • 5 FREE Bonuses
  • Free Upgrades

VYOMS TOP EMPLOYERS

Wipro Technologies
Tata Consultancy Services
Accenture
IBM
Satyam
Genpact
Cognizant Technologies

Home » Articles » ABC's of Delegate

ABC's of Delegate








Article Posted On Date : Thursday, March 22, 2012


ABC's of Delegate
Advertisements

What is Delegate

In the C/C++ languages, function pointers are a buzzword; they can be used to point to a function and call the pointer in order to invoke the function. In .Net, there is nothing called pointer so .Net has delegates. In one sentence, a delegate is a glorified pointer to a function. You can call a static or instance method (function) using traditional classname.methodname or just pointing the method to delegate and call the delegate which, in turn, will call the method.

Delegates provide a mechanism for defining and executing callbacks. This allows you to define the exact signature of a callback and that defines the type of delegate. In general, delegate instances consist of fields like a reference to an object and a pointer to an instance method. However, if the object reference is null, CLR understands it as a static method.

When you declare a delegate, the C# compiler generates a class derived from MulticastDelegate. MulticastDelegate contains several methods but you cannot see those methods if you view the IL of your code in ILDASM. It is because CLR implements all these methods dynamically at runtime.

Delegate allows classes to not be coupled strongly. Also the caller of the delegate has no idea whether it is calling a static method or instance method or what exact method it is calling. Think about the GUI Button class. Button class doesn't know which method it will execute onClick event. Just think, if we write the code within the button class OnClick event, then would it be reusable class? Button class publishes the OnClick event using a delegate and specifies that you can subscribe the OnClick event with a method. In this way, you can de-couple your classes using delegate.

How to call Delegate

Before delving into code, you need to understand one very simple rule about delegates. The return-type or parameter list of the method that you want to reference by delegate, should match with delegate type declaration. Confused! Take an example

Suppose I have one method like void PrintToConsole(string message). The delegate type should be delegate void objDelegate(string message). Now if you write objDelegate = PrintToConsole then it is perfect. Call the function using objDelegate("Hello World") which will execute the PrintToConsole method.

So what does following statement mean?

public delegate void _delegate(int i, int j);

It means a declaration of delegate that can encapsulate any method which will take two integer parameters and return void.

When you declare the above line, C# compiler produces the following code snippet

public class NotifyGpaDelegate : System.MulticastDelegate

    {

        public void Invoke(int GPAScore)

        {

            // code

        }

    }

This Invoke method is part of MulticastDelegate and should have the same signature as the defined delegate.

Simple Delegate

    public delegate void _MathDelegate(int x,int y);

    public class clsMath

    {

        public void Sum(int i, int j)

        {

            Console.WriteLine(i + j);

        }

        public static void Subtract(int i, int j)

        {

            Console.WriteLine(i - j);

        }      

    }

    class Program

    {       

       static void Main(string[] args)

        {

           clsMath _math = new clsMath();

           _MathDelegate _delegate1,_delegate2;

           _delegate1 = _math.Sum;

           _delegate2 = clsMath.Subtract;

           _delegate1(100, 50);

           _delegate2(100, 50);

        }

    }

In the above code, I defined a delegate that returns void and takes two integer parameters. Since I want to call the Sum/Subtract method using this delegate, you can see I have made the signature compatible between delegate and Sum/Subtract method. In my main method, I declared the delegates, referenced the delegates with the methods and called the delegates, which in turn calls the methods. This is an example of a simple delegate.

Delegate Chaining

It means you can maintain a list of delegates which can be called. Internally it maintains a linked list of delegates and when the head of the list is called, all the delegates in the chain are called. In the Delegate class, the following methods help to form a delegate chain.

    public abstract class Delegate : ICloneable, ISerializable

    {

        public static Delegate Combine(params Delegate[] delegates);

        public static Delegate Combine(Delegate a, Delegate b);

    }

The first method takes an array of delegates and returns a delegate type. You can also remove the delegate from delegate chain using the following methods defined in the Delegate class

    public abstract class Delegate : ICloneable, ISerializable

    {

        public static Delegate Remove(Delegate source, Delegate value);

        public static Delegate RemoveAll(Delegate source, Delegate value);

    }

The source is the delegate chain and value is the delegate that you want to remove from delegate chain.

In order to make programming simple, C# compiler uses operator overloading to implement these methods. You can use + to combine and �" to remove from delegate chain.

    public delegate void _MathDelegate(int x,int y);

    public class clsMath

    {

        public void Sum(int i, int j)

        {

            Console.WriteLine(i + j);

        }

        public static void Subtract(int i, int j)

        {

            Console.WriteLine(i - j);

        }      

    }

    class Program

    {       

       static void Main(string[] args)

        {

           clsMath _math = new clsMath();

           _MathDelegate[] _delegate = new _MathDelegate[]{_math.Sum,clsMath.Subtract};

           _MathDelegate _chainDelegate = _delegate[0] + _delegate[1];

           _chainDelegate(100, 50);

        }

    }

In the above example, I define one array of delegates which contains all instance and static methods. You can combine those methods using  + operator which will call Sum and Subtract method respectively.

_MathDelegate _chainDelegate = _delegate[0] + _delegate[1];

       _MathDelegate _chainDelegate1 = _chainDelegate - _delegate[0];

If I use the above two statements, CLR interprets it as (Sum + Subtract) - Sum and executes only the Subtract method. This is an example of removing delegates from the delegate chain using - operator.

You can also iterate through the delegate chain.


    public delegate int _MathDelegate(int x,int y);

    public class clsMath

    {

        public int Sum(int i, int j)

        {

            return (i + j);

        }

        public static int Subtract(int i, int j)

        {

            return (i - j);

        }      

    }

    class Program

    {       

       static void Main(string[] args)

        {

            int iTemp = 0;

           clsMath _math = new clsMath();

           _MathDelegate[] _delegate = new _MathDelegate[]{_math.Sum,clsMath.Subtract};

           _MathDelegate _chainDelegate = _delegate[0] + _delegate[1];

           Delegate[] _delegates = _chainDelegate.GetInvocationList();

           for (int i = 0; i < _delegates.Length; i++)

           {

               _MathDelegate _del = (_MathDelegate)_delegates[i];

               iTemp += _del(100, 50);

           }

           Console.WriteLine(iTemp);          

        }

    }

In the above example, I used GetInvocationList method to get all the delegates in the delegate chain and call each delegate and summed it up. This method helps us to reference each delegate in the delegate chain and we can also call delegates from the delegate chain in any order.

What is Multicast Delegate

A delegate can call more than one method when invoked. This is referred to as multicasting. A useful property of delegate objects is that they can be assigned to one delegate instance to be multicast using the + operator. A composed delegate calls the two delegates it was composed from. Only delegates of the same type can be composed. The - operator can be used to remove a component delegate from a composed delegate. Also multicast delegate return type is always void.

We can change the implementation of the above code because Multicast Delegate return type should be void.

An Example

class Program

    {

        // Simple Sum method

        public static void Sum(int i, int j)

        {

            Console.WriteLine(i + j);

        }

        // Simple Subtract method

        public static void Subtract(int i, int j)

        {

            Console.WriteLine(i - j);

        }

        // Declaring a simple delegate which will

        // encapsulate a Sum/Subtract method

        public delegate void _delegate(int x, int y);

        static void Main(string[] args)

        {

            // Declaring objects of Delegate

            _delegate objDelegate1, objDelegate2, objDelegate3, objDelegate4;

            // Referencing Sum by the delegate object

            objDelegate1 = Sum;

            // Referencing Subtract by the delegate object

            objDelegate2 = Subtract;

            // This will call Sum and Subtract both

            objDelegate3 = objDelegate1 + objDelegate2;

            objDelegate3(100, 50);

            // This will call only Subtract

            objDelegate4 = objDelegate3 - objDelegate2;

            objDelegate4(100, 50);           

        }

    }

objDelegate3 = objDelegate1 + objDelegate2 means it will call Sum and Subtract method both.

objDelegate4 = objDelegate3 - objDelegate2 means (Sum + Subtract) �" Subtract, which means that call to Sum then Subtract and then Sum again.

A few basic things about Delegates

    ·         Delegates are object oriented, type safe and secure.
    ·         Delegate types are derived from Delegate class in .NET.
    ·         Delegate types are sealed so you cannot inherit from Delegate.






Sponsored Ads



Interview Questions
HR Interview Questions
Testing Interview Questions
SAP Interview Questions
Business Intelligence Interview Questions
Call Center Interview Questions

Databases

Clipper Interview Questions
DBA Interview Questions
Firebird Interview Questions
Hierarchical Interview Questions
Informix Interview Questions
Microsoft Access Interview Questions
MS SqlServer Interview Questions
MYSQL Interview Questions
Network Interview Questions
Object Relational Interview Questions
PL/SQL Interview Questions
PostgreSQL Interview Questions
Progress Interview Questions
Relational Interview Questions
SQL Interview Questions
SQL Server Interview Questions
Stored Procedures Interview Questions
Sybase Interview Questions
Teradata Interview Questions

Microsof Technologies

.Net Database Interview Questions
.Net Deployement Interview Questions
ADO.NET Interview Questions
ADO.NET 2.0 Interview Questions
Architecture Interview Questions
ASP Interview Questions
ASP.NET Interview Questions
ASP.NET 2.0 Interview Questions
C# Interview Questions
Csharp Interview Questions
DataGrid Interview Questions
DotNet Interview Questions
Microsoft Basics Interview Questions
Microsoft.NET Interview Questions
Microsoft.NET 2.0 Interview Questions
Share Point Interview Questions
Silverlight Interview Questions
VB.NET Interview Questions
VC++ Interview Questions
Visual Basic Interview Questions

Java / J2EE

Applet Interview Questions
Core Java Interview Questions
Eclipse Interview Questions
EJB Interview Questions
Hibernate Interview Questions
J2ME Interview Questions
J2SE Interview Questions
Java Interview Questions
Java Beans Interview Questions
Java Patterns Interview Questions
Java Security Interview Questions
Java Swing Interview Questions
JBOSS Interview Questions
JDBC Interview Questions
JMS Interview Questions
JSF Interview Questions
JSP Interview Questions
RMI Interview Questions
Servlet Interview Questions
Socket Programming Interview Questions
Springs Interview Questions
Struts Interview Questions
Web Sphere Interview Questions

Programming Languages

C Interview Questions
C++ Interview Questions
CGI Interview Questions
Delphi Interview Questions
Fortran Interview Questions
ILU Interview Questions
LISP Interview Questions
Pascal Interview Questions
Perl Interview Questions
PHP Interview Questions
Ruby Interview Questions
Signature Interview Questions
UML Interview Questions
VBA Interview Questions
Windows Interview Questions
Mainframe Interview Questions


Copyright © 2001-2024 Vyoms.com. All Rights Reserved. Home | About Us | Advertise With Vyoms.com | Jobs | Contact Us | Feedback | Link to Us | Privacy Policy | Terms & Conditions
Placement Papers | Get Your Free Website | IAS Preparation | C++ Interview Questions | C Interview Questions | Report a Bug | Romantic Shayari | CAT 2024

Fresher Jobs | Experienced Jobs | Government Jobs | Walkin Jobs | Company Profiles | Interview Questions | Placement Papers | Companies In India | Consultants In India | Colleges In India | Exams In India | Latest Results | Notifications In India | Call Centers In India | Training Institutes In India | Job Communities In India | Courses In India | Jobs by Keyskills | Jobs by Functional Areas

Testing Articles | Testing Books | Testing Certifications | Testing FAQs | Testing Downloads | Testing Interview Questions | Testing Jobs | Testing Training Institutes

Gate Articles | Gate Books | Gate Colleges | Gate Downloads | Gate Faqs | Gate Jobs | Gate News | Gate Sample Papers | Gate Training Institutes

MBA Articles | MBA Books | MBA Case Studies | MBA Business Schools | MBA Current Affairs | MBA Downloads | MBA Events | MBA Notifications | MBA FAQs | MBA Jobs
MBA Job Consultants | MBA News | MBA Results | MBA Courses | MBA Sample Papers | MBA Interview Questions | MBA Training Institutes

GRE Articles | GRE Books | GRE Colleges | GRE Downloads | GRE Events | GRE FAQs | GRE News | GRE Training Institutes | GRE Sample Papers

IAS Articles | IAS Books | IAS Current Affairs | IAS Downloads | IAS Events | IAS FAQs | IAS News | IAS Notifications | IAS UPSC Jobs | IAS Previous Question Papers
IAS Results | IAS Sample Papers | IAS Interview Questions | IAS Training Institutes | IAS Toppers Interview

SAP Articles | SAP Books | SAP Certifications | SAP Companies | SAP Study Materials | SAP Events | SAP FAQs | SAP Jobs | SAP Job Consultants
SAP Links | SAP News | SAP Sample Papers | SAP Interview Questions | SAP Training Institutes |


Copyright ©2001-2024 Vyoms.com, All Rights Reserved.
Disclaimer: VYOMS.com has taken all reasonable steps to ensure that information on this site is authentic. Applicants are advised to research bonafides of advertisers independently. VYOMS.com shall not have any responsibility in this regard.