Advanced C#

If you want to study C# in depth, keep reading. This ten-part series covers advanced topics to help you sharpen your skills in the language. It is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 5
September 18, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

In this chapter, we cover advanced C# topics that build on concepts explored in previous chapters. You should read the first four sections sequentially; you can read the remaining sections in any order.

Delegates

A delegate dynamically wires up a method caller to its target method. There are two aspects to a delegate: type and instance. A delegate type defines a protocol to which the caller and target will conform, comprising a list of parameter types and a return type. A delegate instance refers to one (or more) target methods conforming to that protocol.

A delegate instance literally acts as a delegate for the caller: the caller invokes the delegate, and then the delegate calls the target method. This indirection decouples the caller from the target method.

A delegate type declaration is preceded by the keyworddelegate, but otherwise it resembles an (abstract) method declaration. For example:

  delegate int Transformer (int x);

To create a delegate instance, you can assign a method to a delegate variable:

  class Test
  {
    static void Main()
    {
      Transformer t = Square;    // create delegate instance
      int result = t(3);           // invoke delegate
      Console.WriteLine (result);
    }
    static int Square (int x) { return x * x; }
  }

Invoking a delegate is just like invoking a method (since the delegate’s purpose is merely to provide a level of indirection):

  t(3);

This statement:

  Transformer t = Square;

is shorthand for:

  Transformer t = new Transformer(Square);

A delegate is similar to a callback, a general term that captures constructs such as C function pointers.

Writing Plug-in Methods with Delegates

A delegate variable is assigned a method dynamically. This is useful for writing plug-in methods. In this example, we have a utility method named Transform that applies a transform to each element in an integer array. TheTransformmethod has a delegate parameter, for specifying a plug-in transform.

  public delegate int Transformer (int x);

  public class Util
  {
    public static void Transform (int[] values, Transformer t)
   
{
    
for (int i = 0; i < values.Length; i++)
       values[i] = t(values[i]);
    }
  }

  class Test
 
{
    static void Main()
    {
     
int[] values = new int[] {1, 2, 3};
      Util.Transform(values, Square);     // dynamically hook in Square
      foreach (int i in values)
        Console.Write (i + " ");         // 1 4 9
    }

    static int Square (int x) { return x * x; }
  }

Multicast Delegates

All delegate instances have multicast capability. This means that a delegate instance can reference not just a single target method, but also a list of target methods. The += operator combines delegate instances. For example:

  SomeDelegate d = SomeMethod1;
  d += SomeMethod2;

Invokingdwill now call bothSomeMethod1 andSomeMethod2. Delegates are invoked in the order they are added.

The-=method removes the right delegate operand from the left delegate operand. For example:

  d -= SomeMethod1;

Invokingdwill now cause onlySomeMethod2to be invoked.

Calling+=on a delegate variable with anullvalue works, and it is equivalent to assigning the variable to a new value:

  SomeDelegate d = null;
  d += SomeMethod1;       // equivalent (when d is null) to d = SomeMethod1;

If a multicast delegate has a nonvoid return type, the caller receives the return value from the last method to be invoked. The preceding methods are still called, but their return values are discarded. In most scenarios in which multicast delegates are used, they havevoid return types, so this subtlety does not arise.

All delegate types implicitly inheritSystem.MulticastDelegate, which inherits fromSystem.Delegate. C# compiles+=and-=operations made on a delegate to the staticCombineandRemovemethods of theSystem.Delegateclass.

Multicast delegate example

Suppose you wrote a routine that took a long time to execute. That routine could regularly report progress to its caller by invoking a delegate. In this example, the HardWork routine has a ProgressReporterdelegate parameter, which it invokes to indicate progress:

  public delegate void ProgressReporter (int percentComplete);

  public class Util
  {
   
public static void HardWork (ProgressReporter p)
   
{
     
for (int i = 0; i < 10; i++)
     
{
       
p (i * 10);              // Invoke delegate
       
System.Threading.Thread.
Sleep(100);                      // Simulate hard work
     
}
    }
  }

To monitor progress, theMainmethod creates a multicast delegate instancep, such that progress is monitored by two independent methods:

  class Test
 
{
   
static void Main ()
   
{
     
ProgressReporter p = WriteProgressToConsole;
     
p += WriteProgressToFile;
      Util.HardWork (p);
    }

    static void WriteProgressToConsole (int percentComplete)
    {
      Console.WriteLine (percentComplete);
    }

    static void WriteProgressToFile (int percentComplete)
    {
      System.IO.File.WriteAllText ("progress.txt", percentComplete.
          ToString());
    }
  }

Instance Method Targets

When a delegate instance is assigned to an instance method, the delegate instance must maintain a reference not only to the method, but also to the instance of that method. The System.Delegate class’s Targetproperty represents this instance (and will be null for a delegate referencing a static method). For example:

  public delegate void ProgressReporter (int percentComplete);

  class Test
  {
    static void Main() {new Test();}
    Test ()
    {
      ProgressReporter p = InstanceProgress;
      p(99);                         // 99
      Console.WriteLine (p.Target == this);                               // True
      Console.WriteLine (p.Method);  // Void InstanceProgress(Int32)
    }

    void InstanceProgress (int percentComplete)
    {
      Console.WriteLine(percentComplete);
    }
  }

Generic Delegate Types

A delegate type may contain generic type parameters. For example:

  public delegate T Transformer<T> (T arg);

With this definition, we can write a generalizedTransformutility method that works on any type:

  public class Util
  {
    public static void Transform<T> (T[] values,
Transformer<T> t)
    {
      for (int i = 0; i < values.Length; i++)
        values[i] = t(values[i]);
    }
  }
 

  class Test
  {
    static void Main()
    {
     
int[] values = new int[] {1, 2, 3};
      Util.Transform(values, Square);     //
dynamically hook in Square
      foreach (int i in values)
        Console.Write (i + " ");         // 1 4 9
    }

    static int Square (int x) { return x * x; }
  }

Delegates Versus Interfaces

A problem that can be solved with a delegate can also be solved with an interface. For instance, the following explains how to solve our filter problem using an ITransformer interface:

  public interface ITransformer
  {
    int Transform (int x);
  }

  public class Util
  {
   public static void TransformAll (int[] values, ITransformer t)
   {
    
for (int i = 0; i < values.Length; i++)
       values[i] = t.Transform(values[i]);
   }
  }

  class Test : ITransformer
  {
   static void Main()
   {
    
int[] values = new int[] {1, 2, 3};
     Util.TransformAll(values, new Test());
     foreach (int i in values)
       Console.WriteLine (i);
   }

   public int Transform (int x) { return x * x; }
  }

A delegate design may be a better choice than an interface design if one or more of these conditions are true:

  1. The interface defines only a single method.
  2. Multicast capability is needed.
  3. The listener needs to implement the interface multiple times.

In theITransformerexample, we don’t need to multicast. However, the interface defines only a single method. Furthermore, our listener may need to implementITransformermultiple times, to support different transforms, such as square or cube. With interfaces, we’re forced into writing a separate type per transform, sinceTestcan only implementITransformeronce. This is quite cumbersome:

  class Test

  {
   static void Main()
   {
    
int[] values = new int[] {1, 2, 3};
     Util.TransformAll(values, new Cuber());
     foreach (int i in values)

       Console.WriteLine (i);
   }

   class Squarer : ITransformer
   {
    
public int Transform (int x) { return x * x; }
   }
   class Cuber : ITransformer
   {
    
public int Transform (int x) {return x * x * x; }
   }
  }

Please check back next week for the continuation of this article.

blog comments powered by Disqus
C# ARTICLES

- Beginning C#
- ASP.NET RedirectPermanent Method using C# an...
- C Programming Language and UNIX Pioneer Pass...
- Using Facebook JavaScript SDK in ASP.NET wit...
- ASP.NET Export to Excel and Word using VB.NE...
- WAV and MP3 Streaming with ASP.Net and C#
- Game Programming using SDL: the File I/O API
- C# and Java Developer Jobs on the Rise
- The Future Evolution of C# and VB.NET
- C# If and Else-if Statements
- How To Use the C# String Replace Method
- 5 Ways to Parse XML in C#
- C# Meets Design Patterns
- Coding a CRC-Generating Algorithm in C
- Cyclic Redundancy Check

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials