C# 2.0 Features - Part 2.2 Anonymous Methods

Feb
19
2010
In Categories: .NET | CLR
Tags | |

You can see part 1 here and part 2.1 here

I always thought that Anonymous Methods came out with .NET framework version 3.0 Embarassed when I dig more I reliased that it came up with version 2.0.

Anonymous Method means that you can create a method - block of code - without giving it a name and execute it. The question is how can I execute it? And why do I need such a feature.

We always use Anonymous Methods with delegates although I don't like it as such. I consider Anonymous Methods is the step just before lambda methods. About the usage I only see its benifit in small programs where you try something. From my point of view I don't recommend using it as it may lead to bugs that may be hard to trace. Use it with care.

Here how can we us Anonymous Methods  

 

    1 using System;
    2 
    3 namespace ConsoleApplication2
    4 {
    5     class Program
    6     {
    7         delegate int Square(int x);
    8 
    9         static void Main()
   10         {
   11             Square square = delegate(int power)
   12                                 {
   13                                     return power*power;
   14                                 };
   15 
   16             Console.WriteLine(square(10));
   17         }
   18     }
   19 }

 

Comments

trackback
Technical Architect blog
5/21/2010 10:21:57 PM Permalink

C# 2.0 Features - Part 2.5 Partial classes

C# 2.0 Features - Part 2.5 Partial classes

Comments are closed