SIGN UP MEMBER LOGIN:    
ARTICLE

C# Anonymous method

Posted by Dhananjay Kumar Articles | C# Language March 16, 2009
This article discusses the basics of anonymous methods in C# and how to take advantage of them by implementing them in your applications.
Reader Level:

This article discusses the basics of anonymous methods in C# and how to take advantage of them by implementing them in your applications.

An anonymous method is a block of code that is used as the parameter for the Delegate.

Anonymous method allows coder to pass block of code rather than the name of the method. Creating a anonymous method is a way to pass a code block as delegate parameter.

Benefits:

  1. To reduce code the amount of code 
  2. To reduce complexity of code. 
  3. To increase readability of code. 
  4. Anonymous method can use any local variable declared outside of method.

Rules

  1. Cannot have jump statement that targets outside the anonymous method. 
  2. Jump statement from outside of anonymous method cannot target inside anonymous method. 
  3. Anonymous method cannot use Ref and Out parameter from outside of anonymous method.

Synatx:

Button1.Click+= delegate(Object o, EventArgs e)
{
      MessageBox.Show("SCOTT"); 
}

Example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication4

{

    class Program

    {

        // Decelaring Delegate

        public delegate void Display(String msg);

        static void Main(string[] args)

        {

            // Delegate using anonymous method

            Display del = delegate(string par)

            Display del = delegate(string par)

            {

                Console.WriteLine("{0}", par);

            };

            // Invoking delegate

            del("Christ");

            Console.ReadLine();

        }

    }

}

In above code, Christ will get display as output.  In above code, block of code is getting passes as argument of delegate rather than name of the method.

Scope of the variable

The scope of variable of an anonymous method is the anonymous method block.

Anonymous method does not help speeding up the execution process because for block of code, .Net generates the Anonymous name.

Login to add your contents and source code to this article
share this article :
post comment
 

I am so sick of console applications. Does anyone REALLY use these things outside of examples? How about something useful, like a web app, hey people USE those!

Daniel: agree.  However, your example you suggest is just as bad in writing, and...still, uses console apps.  Thumbs down.

Posted by Allan Mar 30, 2009

While I think that it is good to copy and spread information I think professional practice is to cite the references. I read a very thourough explaination of anonymous method and why and how it works and then I see two articles on the subject Not so clear, not so detailed. No references to say where it was learnt. What am I to think?

Posted by lynne whitaker Mar 29, 2009

Daniel, I agree completely. This looks more like "hey, I have an article on C# Corner" than something really useful. The description and example are both weak. In fact, I think the use of a delegate actually clouds what is going on in this case and would rather see a method used. Two thumbs down.

Posted by Chris Mar 27, 2009

Besides the obvious mistakes like "Synatx" and "Decelaring" this article seems to be put together too quickly to really be helpful - it leaves reader scratching head still. For a better example, check this out: http://shiman.wordpress.com/2008/09/14/anonymous-method-in-c-net/

Posted by Daniel Hughes Mar 27, 2009
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor