Translate

Tuesday 11 December 2007

Lambda Expressions

It was in 2006 that I heard from a remote place called Mohali that there was a likelihood of a new type called the inferred type in the new version of .Net. At the time this term did not cause as much alarm as did something called "Lambda Expressions"! Looking back over the last few years since .Net came in, .Net has kinda stunned you at first sight with new terminology. I remember writing somewhere it is all about new terminology only and there was nothing new actually. I guess I was wrong to some extent and right to some because the need to create new terminology arises from identifying new technology and introducing them in the right manner to a new audience. Like for instance, the LINQ. At first sight, it is not something that you will fall in love with but expanding it to "Language Integrated Query" makes your life suddenly easier.

Lambda Expressions are somewhat similar. Looked at individually, where c=> x==y;, they would seem to be the toughest things doing the rounds but if you progress from the .Net 1.1 version's way of calling methods through delegates to anonymous methods in 2.0, the expressions make a lot of sense for its conciseness.

If in .net 1.1,

public delegate void MyDelegate();
public class TestDel{
public void IncreaseValue(){
Console.WriteLine("The value has increased");
}
private static void SomeMethod(int x){
MyDelegate del;
if (x>10)
del=new MyDelegate(IncreaseValue);
else
del=new MyDelegate(ValueDecrease); // ValueDecrease() not defined
del();
}
public static void Main(){
SomeMethod(12);
}

More to follow...