C#
  Home arrow C# arrow Page 4 - 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# - Delegates Versus Interfaces


    (Page 4 of 4 )

    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 6 Hosted by Hostway
    Stay green...Green IT