C# Anonymous Methods and Lambda Expressions

In the following example talks about anonymous methods, method group conversions and finally lambda expressions.

using System;

using System.Collections.Generic;

public class CarEventArgs : EventArgs

{

public string Msg { get; set; }

public CarEventArgs(string msg)

{

Msg = msg;

}

}

public class Car

{

private int _speed = 0;

public event EventHandler<CarEventArgs> MyEvent;

public void Acccelarate(int speed)

{

_speed += speed;

if (MyEvent != null)

{

MyEvent(this, new CarEventArgs(string.Format(“Current speed {0}”, _speed)));

}

}

}

public class MyClass

{

public static void RunSnippet()

{

Car c = new Car();

//traditional event method call.

c.MyEvent += new EventHandler<CarEventArgs>(TestMethod);

//c# method group conversion, let the compiler figure out the delegate type from method signature.

c.MyEvent += TestMethod;

//c# anonymous method — associate delegate directly to block of code statements at the time of event regristration.

c.MyEvent += delegate(object sender, CarEventArgs args) { WL(“from Anonymous method.” + args.Msg); };

//Lamda expression: more consise way of authoring anonymous methods.

//lamda expression syntax: ArgumentsToProcess => statementsToProcessThem;

c.MyEvent += (s, e) => { WL(“lamda “ + ((CarEventArgs)e).Msg); };

c.MyEvent += (object s, CarEventArgs e) => { WL(“lamda with variables declared:” + e.Msg); };

c.Acccelarate(10);

RL();

}

public static void TestMethod(object sender, CarEventArgs args)

{

WL(args.Msg);

}

#region Helper methods

public static void Main()

{

try

{

RunSnippet();

}

catch (Exception e)

{

string error = string.Format(“—\nThe following error occurred while executing the snippet:\n{0}\n—“, e.ToString());

Console.WriteLine(error);

}

finally

{

Console.Write(“Press any key to continue…”);

Console.ReadKey();

}

}

private static void WL(object text, params object[] args)

{

Console.WriteLine(text.ToString(), args);

}

private static void RL()

{

Console.ReadLine();

}

private static void Break()

{

System.Diagnostics.Debugger.Break();

}

#endregion

}

Advertisements

About Shashi Kuppa

I have 14 years of Information System experience. My background ranges from software architecture, analysis, design and development to team leadership and project management. My primary strengths include .Net system architecture, object oriented development using C#, web development using Asp.net and SQL server development, reporting and integration. I am an excellent team player, good communicator and have a proven track record for goal-oriented project leadership and management
This entry was posted in Development, Uncategorized and tagged . 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 )

Google+ photo

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

Connecting to %s