Click here to Skip to main content
15,894,343 members
Articles / Programming Languages / C#

C#2 Anonymous Methods

Rate me:
Please Sign up or sign in to vote.
4.78/5 (33 votes)
15 Jun 200613 min read 112.6K   397   96  
This article presents a new feature named anonymous methods added to the 2.0 version of the C# language. Unlike generics, this feature does not involve new IL instructions. All the magic happens at the level of the compiler.
//
// Extracted from the book Practical .NET2 and C#2 by Patrick Smacchia
// http://www.practicaldot.net/en2/Chapter_14/Listing_14_25.htm
//
class Program {
   delegate void DelegateType();
   static void Main() {
      DelegateType delegateInstance = delegate() {
         System.Console.WriteLine("Hello");
      };
      delegateInstance();
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
Patrick Smacchia is a .NET MVP involved in software development for over 15 years. He is the author of Practical .NET2 and C#2, a .NET book conceived from real world experience with 647 compilable code listings. After graduating in mathematics and computer science, he has worked on software in a variety of fields including stock exchange at Société Générale, airline ticket reservation system at Amadeus as well as a satellite base station at Alcatel. He's currently a software consultant and trainer on .NET technologies as well as the author of the freeware NDepend which provides numerous metrics and caveats on any compiled .NET application.

Comments and Discussions