Simplest C# Anonymous Method

Simplest C# Anonymous Method

Refer to the article Create Elegant Code With Anonymous Methods, Iterators, And Partial Classes (http://msdn.microsoft.com/msdnmag/issues/06/00/C20/default.aspx). There is a particular small section I like it very much, Anonymous Methods. This feature is related to the concept of Lambda expressions of LINQ (http://msdn2.microsoft.com/en-us/library/aa730866(VS.80).aspx).

When you download the source code for the article then you will realize the Anonymous Method part is not the simplest to understand. I have rebuilt the sample code to put delegate with and without using Anonymous Method together to have better look and feel.

class Program
  {
    delegate void SomeDelegate();
 
    public static void InvokeMethodwoAnonymous()
    {
      SomeDelegate del = new SomeDelegate(SomeMethod);
      del();
    }
 
    public static void SomeMethod()
    {
      Console.WriteLine("Hello without Anonymous");
    }
 
    public static void InvokeMethodwAnonymous()
    {
      SomeDelegate del = delegate()
                          {
                            Console.WriteLine("Hello with Anonymous");
                          };
      del();
    }
 
    static void Main(string[] args)
    {
      InvokeMethodwoAnonymous();
      InvokeMethodwAnonymous();
    }
  }

 Do modify the code to play following the mentioned article. For more information go to:

http://msdn2.microsoft.com/en-us/vcsharp/default.aspx

http://blogs.msdn.com/charlie/

 

About these ads

About chanmingman

Ming Man is a senior manager for a development company. With 20 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s