Close AD
Anonymous Methods/Delegates in C# .Net 2.0 - Chintan Patel's Blog

Chintan Patel's Blog
Home | Archive | Links | Contact Sign In | Sign Up

About Author

Chintankumar Patel
09 Jun, 2008

Contact Me  

Working as a Technical Consultant for Conchango.

Having experience in to IT from 7+ years and working on Microsoft Technologies

Archive

2008 Oct   (3)
2008 Sep   (6)
2008 Aug   (1)
2008 Jul   (2)
2008 Jun   (7)

Recent Posts

What's New in the .NET Framework 2.0 ?
Comments : 0
Not Rated  
How to work with partitions in Windows Vista / XP when Disk Management doesn’t work
Comments : 0
Not Rated  
How to resize a partition in Windows Vista?
Comments : 0
Not Rated  
Top 10 tricks for handling null values in Microsoft Office Access
Comments : 0
Not Rated  
What is Vista's ReadyBoost and SuperFetch Technology
Comments : 0
Not Rated  
What is YouTube? - An introduction to the YouTube.com
Comments : 1
Not Rated  
SQL DATEDIFF Function - Applies to MS SQL Server and MS Office Access
Comments : 0
Not Rated  
Booting from USB Pen/Key/Flash Drive (Windows/Linux)
Comments : 1
Not Rated  
How to Convert FAT/FAT32 to NTFS file system
Comments : 0
Not Rated  
Resizing Images to match it's Scale in C# .Net
Comments : 5
Not Rated  

Categories

.Net Graphics   (2)
.Net Technology   (6)
ASP.Net   (1)
General   (1)
Microsoft Access   (1)
Microsoft Visual Studio   (1)
Microsoft Windows   (4)
SQL   (1)
USB   (1)
Windows Vista   (1)

Tags

.Net , .Net Framework 2.0 , .Net Graphics , Asp.Net , Boot , C# , Class Library , Coding Standards , Convert File System , Database , DATEDIFF , DATEDIFF Function , Disk Management , Embedded Resources , EnableEventValidation , EnableViewState , EncoderParameter , FAT32 to NTFS , Form Authentication , HDD , High Quality Thumbnail , Intellisense , Linux , Microsoft Access , Microsoft's SQL , Partition , ReadyBoost , ReSharper , Resize Image , Resize Partition , Security , SQLDataReader , SuperFetch , USB , Usb device not recognized , Vista , Visual Studio 2005 , What's New , Windows , Windows Application , Windows Errors , Windows Vista , Windows XP , YouTube

Anonymous Methods/Delegates in C# .Net 2.0

Anonymous Methods/Delegates in C# .Net 2.0

Fri, 20 Jun, 2008

 

C# 2.0 provides a new feature called Anonymous Methods, which allow you to create inline un-named ( i.e. anonymous ) methods in your code, which can help increase the readability and maintainability of your applications by keeping the caller of the method and the method itself as close to one another as possible. This is akin to the best practice of keeping the declaration of a variable, for example, as close to the code that uses it.

Here is a simple example of using an anonymous method to find all the even integers from 1...10:

 

private int[] _integers =
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
}; int[] evenIntegers = Array.FindAll(_integers,
delegate(int
integer) { return (integer%2 == 0); } );

 

The Anonymous Method is:

 

 delegate(int integer)
    {
       
return (integer%2 == 0
);
    }

 

which is called for each integer in the array and returns either true or false depending on if the integer is even.

If you don't use an anonymous method, you will need to create a separate method as such:

 

private int[] _integers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int[] evenIntegers = Array.FindAll(_integers, IsEven);

private bool IsEven(int integer)
{
    return (integer%2 == 0);
}

 

When you have very simple methods like above that won't be reused, I find it much more elegant and meaningful to use anonymous methods. The code stays closer together which makes it easier to follow and maintain.

Here is an example that uses an anonymous method to get the list of cities in a state selected in a DropDownList ( called States ):

 

List<City> citiesInFlorida =
cities.FindAll(delegate(City city)
     {
         return city.State.Name.Equals(States.SelectedValue);
     }
);

 

You can also use anonymous methods as such:

 

button1.Click +=

    delegate
        {
            MessageBox.Show("Hello");
        };

 

which for such a simple operation doesn't “deserve“ a separate method to handle the event.

Other uses of anonymous methods would be for asynchronous callback methods, etc.

Anonymous methods don't have the cool factor of Generics, but they do offer a more expressive in-line approach to creating methods that can make your code easier to follow and maintain.


  

Anonymous Methods/Delegates in C# .Net 2.0


Comments

No Comments found.


Add Comment
 
 
 
 
 
 
 
 

Your Email address will not be displayed on the website, It is optional, if provided you will get an alert when some comments on this Post.
 
 
 
 
Remaining 5000
 

Powered by NineOn Inc.