Delegate pattern vs delegate keyword in C# -


from msdn doc:

a delegate type safely encapsulates method, similar function pointer in c , c++. unlike c function pointers, delegates object-oriented, type safe, , secure.

i know , how use it. wonder whether or not written base on delegate pattern know (from wikipedia) .
difference between them?

the c# (not pattern) delegate might useful when implementing delegate pattern @ delegate pattern implementation wikipedia changes:

//note: sample, not suggestion in such way  public interface {     void f();     void g(); }  public static class {     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 : {     // 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 useful here, isn't introduced. should @ on low-level construction rather pattern. in pair events used implement publisher/subscriber(observer) pattern - @ this article, or implement visitor pattern - actively used in linq:

public void linq1()  {      int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };       // n => n < 5 lambda function, creates delegate here     var lownums = numbers.where(n => n < 5);       console.writeline("numbers < 5:");      foreach (var x in lownums)      {          console.writeline(x);      }  }  

to summarize: language delegate not pattern itself, allows operate functions first class objects.


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -