Friday, February 22, 2008

Extension methods and Lambda expressions

Maybe it’s a bit late for me to post about .Net 3.5 + VS 2008, but that’s what I am exactly doing these days.
My first interest in the language was on Lambda expressions and extension methods and then LINQ.
I have learnt quite some stuff about LINQ, meaning the basic, but still did not have a time to try out LINQ to SQL, maybe I will, in the coming week.

Just to give you an idea about Lambda expressions, this is an evolution of anonymous methods in .Net 2.0 and this comes in handy when using LINQ. You can read more about Lambda function here.
Extension methods are really cool stuff, this allow you to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type. So for an example you can write a extension method like Print() and add it to the string class and you can use is it like this.

String str = “Say Hello World”;
You can read more about Extension methods here.

Another thing that I tried out was the object Initialize feature, this allows you to set field values when creating objects.Note that the actual field initialization occurs after calling the default ctor. For an example I can do this ...
Student stu = new Student{FirstName = "Nairooz", LastName = "NIlafdeen"};
you can read more about this here

No comments:

Post a Comment