C#
  Home arrow C# arrow Page 2 - Advanced C#
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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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#

Advanced C#
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-09-18

    Table of Contents:
  • Advanced C#
  • Writing Plug-in Methods with Delegates
  • Instance Method Targets
  • Delegates Versus Interfaces

  • 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


    Advanced C# - Writing Plug-in Methods with Delegates


    (Page 2 of 4 )

    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());
        }
      }

    More C# Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "C# 3.0 in a Nutshell, Third Edition, A...
     

    Buy this book now. This article 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). Check it out today at your favorite bookstore. Buy this book now.

    C# ARTICLES

    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT