Geeks With Blogs
Dheeman Dutta Just Another Blog.....

This topic is quite a common topic on the net after c# 2.0 has been released. ( quite  a fews months before) . With the introduction of c# 2.0 came in the concept of Anonymous methods and Anonymous delegates. Delegates , previously needed o have named methods to point to. With this new introduction , adds another flexibilty.... The code snippet below shows both ways ( the named method and the anonymous way)

class Program

{

delegate void TestAnonDelegate(string foo);

static void Main(string[] args)

{

//delegate instatiation --the old way

TestAnonDelegate O = new TestAnonDelegate(DoWork);

O("In method do work");

//delegate instatiation --the new way

TestAnonDelegate t = delegate(string foo) { Console.WriteLine(foo); Console.ReadKey(); }; 

// note that the delegate keyword is used

t("in anonymous delegate");

}

static void DoWork(string s)

{

Console.WriteLine(s);

Console.ReadKey();

}

}

Both do the same job but ne with a named method from beforehand and one without. There are some limitations that I know of anonymous delegates . They are...

i) they cannot access out,ref parameters of a oute scope.

ii) no break,continue,goto is allowd within anonymous methods

 

If u know of any other limitations , do post me a comment

Cheers................

 

Posted on Thursday, November 16, 2006 5:24 AM | Back to top


Comments on this post: Anonymous Methods and delegates

# re: Anonymous Methods and delegates
Requesting Gravatar...
good example
Left by Shubhajyoti on Feb 13, 2007 4:58 AM

Your comment:
 (will show your gravatar)


Copyright © Dheeman Dutta | Powered by: GeeksWithBlogs.net