C#
  Home arrow C# arrow Page 3 - C# Delegates Explained
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 
Moblin 
JMSL Numerical Library 
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 / 56
    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


    C# Delegates Explained - Delegates on Instance Methods


    (Page 3 of 5 )

    Copy the following code and paste it in your VS.NET class file

    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 aDelegate = new Math.DelegateToMethod(math.Add);
          Math.DelegateToMethod mDelegate = new Math.DelegateToMethod(math.Multiply);
          Math.DelegateToMethod dDelegate = new Math.DelegateToMethod(math.Divide);
          aDelegate(5,5);
          mDelegate(5,5);
          dDelegate(5,5);
          Console.ReadLine();
        }
      }
    }

    I have made some changes to the example so let's go through them. First I have defined the delegate type as a nest type to the Math class. This is perfectly valid but you have to qualify the name of the delegate with the class name, so we write Math.DelegateToMethod.

    I have changed the signature of the delegate so it returns void instead of int. I had to change the signature of the methods of the Math class to return void. I also removed the static keyword.

    In the Main method of the class DelegateApp I created an object of the Math class, then I created the three delegate objects, but this time I passed an instance method (not a static method). I called the delegate and passed 5, 5 as the parameters.

    I think by now you understand the mechanism of how delegates work, but you don't understand why it's useful or how we can use them efficiently. Just wait until the next article (C# Events) and you will appreciate the use of delegates. Now let's discuss multicast delegates.

    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...
       · Yes, the article contains many technical details. But it's a preliminary to the...
       · Since 2.0 the invocation linked list had beeen replaced by an array
     

    C# ARTICLES

    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT