Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Working with Delegates in C#
By Joe Mayo

Rate This Article: Add This Article To:

Working with Delegates in C# - ' A Quick Example '
( Page 2 of 6 )



A Quick Example

To put delegates to use, you need to define one or more handler methods, assign the handler when instantiating a delegate, and perform an invocation on the delegate instance. The example in Listing 1 shows a very simple way to use a delegate.

Listing 1. QuickDelegateDemo.cs

using System;

// 1. Define delegate.
public delegate double UnitConversion(double from);

class QuickDelegateDemo
{
    // 2. Define handler method.
    public static double FeetToInches(double feet)
    {
        return feet * 12;
    }

    static void Main()
    {
        // 3. Create delegate instance.
        UnitConversion doConversion = new UnitConversion(FeetToInches);

        Console.WriteLine("Feet to Inches Converter");
        Console.WriteLine("------------------------\n");
        
        Console.Write("Please enter feet:  ");
        double feet = Double.Parse(Console.ReadLine());

        // 4. Use delegate just like a method.
        double inches = doConversion(feet);

        Console.WriteLine("\n{0} Feet = {1} Inches.\n", feet, inches);
        Console.ReadLine();
    }
}

There are four steps in Listing 1, demonstrating the mechanics of using a delegate in very simple terms. It is framed around an application that performs conversions between length units of Feet and Inches. Step 1 declares the delegate as described in the previous section, Delegate Signatures.

Step 2 defines the delegate handler method, FeetToInches. Notice the signature of this method: it accepts a single parameter of type double and returns a value of type double. This matches the signature of the UnitConversion delegate, defined in Step 1. The method identifier, FeetToInches, and the parameter identifier, feet, are descriptive of the purpose of the method and have no relation to the delegate identifiers. It is only the type, number, and order of parameters and the return type that defines the signature of a compatible delegate handler method.

A delegate must be instantiated before it can be used. As shown in Step 3, the doConversion delegate is instantiated as type UnitConversion, similar to any other type declaration. Its constructor has a single parameter that identifies a handler method to be assigned to the delegate. The delegate parameter is only the identifier of the handler without parenthesis or handler parameters.

To invoke the method referred to by the delegate; the delegate instance is used, as if it were the method, as shown in Step 4. You would simply use the delegate instance and pass it the applicable parameters, causing the delegate to invoke its underlying handler method with the same parameter that was passed to the delegate.



 
 
>>> More Microsoft Languages Articles          >>> More By Joe Mayo