Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate pattern vs delegate keyword in C# [closed]

From MSDN doc:

A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure.

I know what it is and how to use it. But I wonder whether or not it is written base on delegate pattern that I know (from wikipedia) .
What is the difference between them?

like image 780
ductran Avatar asked Jun 13 '12 07:06

ductran


People also ask

What is delegate keyword?

delegate: It is the keyword which is used to define the delegate. return_type: It is the type of value returned by the methods which the delegate will be going to call. It can be void. A method must have the same return type as the delegate. delegate_name: It is the user-defined name or identifier for the delegate.

What is difference between delegate and method?

A delegate is a method with a parameter and a return type. A delegate is a type that safely encapsulates a method. Delegates are object-oriented, type safe, and secure.

What is the difference between delegate and event?

Events Have Private Invocation Events are typically public class members. By comparison, delegates are often passed as parameters and stored as private class members, if they are stored at all.

What is delegate in C?

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.


2 Answers

The delegation pattern is:

a design pattern [...] where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object.

Unfortunately, that page does not describe much about when to use it or what patterns derive from it, apart from

The delegation pattern is one of the fundamental abstraction patterns that underlie other software patterns such as composition (also referred to as aggregation), mixins and aspects.

From the page that describes delegation, you can figure that it is to delegate the implementation of a feature to a class that may or may not be known at runtime. When you say Foo.Bar(), the implementation of that may delegate the execution of Bar() to the beforementioned "helper object".

Now for the C# delegate, as stated, that's simply a function pointer. It can help implement a delegation pattern, by assigning the delegation method at either compile time or runtime.

like image 22
CodeCaster Avatar answered Sep 29 '22 02:09

CodeCaster


The C# (not the pattern) delegate might be useful when you are implementing the delegate pattern just look at this delegate pattern implementation from wikipedia with my changes:

//NOTE: this is just a sample, not a suggestion to do it in such way

public interface I
{
    void F();
    void G();
}

public static class A
{
    public static void F() { System.Console.WriteLine("A: doing F()"); }
    public static void G() { System.Console.WriteLine("A: doing G()"); }
}

public static class B
{
    public static void F() { System.Console.WriteLine("B: doing F()"); }
    public static void G() { System.Console.WriteLine("B: doing G()"); }
}

public class C : I
{
    // delegation 
    Action iF = A.F;
    Action iG = A.G;

    public void F() { iF(); }
    public void G() { iG(); }

    // normal attributes
    public void ToA() { iF = A.F; iG = A.G; }
    public void ToB() { iF = B.F; iG = B.G; }
}

public class Program
{
    public static void Main()
    {
        C c = new C();
        c.F();     // output: A: doing F()
        c.G();     // output: A: doing G()
        c.ToB();
        c.F();     // output: B: doing F()
        c.G();     // output: B: doing G()
    }
}

Again delegate might be useful here, but it isn't for it was introduced. You should look at it like on the low-level construction rather then the pattern. In the pair with the events it could be used to implement publisher/subscriber(observer) pattern - just look at this article, or it could sometimes help you to implement visitor pattern - this is actively used in the LINQ:

public void Linq1() 
{ 
    int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 

    // n => n < 5 is lambda function, creates a delegate here
    var lowNums = numbers.Where(n => n < 5); 

    Console.WriteLine("Numbers < 5:"); 
    foreach (var x in lowNums) 
    { 
        Console.WriteLine(x); 
    } 
} 

To summarize: a language delegate is not the pattern itself, it just allows you to operate functions as the first class objects.

like image 124
ie. Avatar answered Sep 29 '22 04:09

ie.