SunQuest
 
       C#
  Home arrow C# arrow Page 4 - C# Delegates Explained
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

C# Delegates Explained
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 47
    2007-02-07

    Table of Contents:
  • C# Delegates Explained
  • Delegates and their Generated MSIL Code
  • Delegates on Instance Methods
  • Multicast Delegates
  • Callback Methods through Delegates

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    C# Delegates Explained - Multicast Delegates


    (Page 4 of 5 )

    As you saw in the MSIL code, the delegate type that we created automatically inherits the System.MulticastDelegate, which provides a functionality that creates a chain of delegates through a linked list. It's better to explain with an example, so let's modify our example to use a multicast delegate.

    using System;

    namespace Delegates
    {
      public class Math
      {
        // note that the delegate now is a nested type of the Math class
        public delegate void DelegateToMethod(int x, int y);

        public void Add(int first, int second)
        {
          Console.WriteLine("The method Add() returns {0}", first + second);
        }

        public void Multiply(int first, int second)
        {
          Console.WriteLine("The method Multiply() returns {0}", first * second);
        }

        public void Divide(int first, int second)
        {
          Console.WriteLine("The method Divide() returns {0}", first / second);
        }
      }

      public class DelegateApp
      {
        public static void Main()
        {
          Math math = new Math();
          Math.DelegateToMethod multiDelegate = null;
          multiDelegate = new Math.DelegateToMethod(math.Add);
          multiDelegate += new Math.DelegateToMethod(math.Multiply);
          multiDelegate += new Math.DelegateToMethod(math.Divide);
          multiDelegate(10,10);
          Console.ReadLine();
        }
      }
    }

    A multicast delegate is an object that maintains a linked list of delegates. Invoking the delegate invokes each delegate (which in turn calls its encapsulated method) in the same order that it has been added to the linked list. In our example we have created an object (a delegate) called multiDelegate of type Math.DelegateToMethod and assigned a null value to this object.

    The next statement assigns (through the assignment = operator as you already know) a new delegate object that encapsulates the method math.Add(). Using the operator += we assign more delegate objects (thus creating a multicast delegate) to the delegate multiDelegate. Then invoking the delegate invokes all the delegates maintained in its linked list, which in turn calls the encapsulated methods.

    The statement multiDelegate(10,10); is similar to our previous example in which we have created three delegate objects and then calling each of them separately. You can remove a delegate object from the linked list using the operator -=. Actually, behind the scenes the method Combine() is used to perform the creation of a multicast delegate. In fact this method returns a new delegate instance. If you load the application with ILDASM you will see the use of this method.

    More C# Articles
    More By Michael Youssef


       · Understanding Delegates is vital to help understanding Events. In this article, you...
       · great article as all of your articles. Please write more about C# 2.0 and again more...
       · Hi,If you want to ask me a question that is not related to the article then sure...
       · Good article but Youssef goes into too much detail explaining the delegates. It...
     

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained

    Iron Speed




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway