Cook Computing

Delegate-based APIs

June 23, 2004 Written by Charles Cook

Krzysztof Cwalina describes some new delegate-based APIs added to the .NET collection libraries. A couple of points came to mind on reading this.

One, would it be better if the methods returning List<ItemType> instead returned IEnumerator<ItemType>, using an iterator for the implementation? This would give you more flexibility in what you do with the output.

Two, just a minor point, but I'd not paid much attention before to the fact that the type of an anonymous method is inferred by the compiler. Along these lines delegate inference is used to simplify the syntax used for assigning to delegate variables. Instead of this:


class SomeClass
{
   delegate void SomeDelegate();
   public void InvokeMethod()
   {
      SomeDelegate del = new SomeDelegate(SomeMethod);
      del();
   }
   void SomeMethod()
   {...}
}

you can now write:


class SomeClass
{
   delegate void SomeDelegate();
   public void InvokeMethod()
   {
      SomeDelegate del = SomeMethod;
      del();
   }
   void SomeMethod()
   {...}
}