Saturday, October 25, 2008

Dot Net Framework 3.5

In this article I will discuss about some of the language enhancements in C# 3.0, which is bundled with Dot Net Framework 3.5

Extension Methods

In almost all the projects you have done so far, there is no doubt that you had a “Utility” class or “Utility” project to do data type conversions, data validations etc. Extension methods allow you to do this in a cool new fashion! When you are using your old fashioned Utility class (let’s say that you are using a method to check null values) you should have following code.

int empID =10;
DataTable dtEmpData= ServiceObject.GetEmpData(10);
bool blnValue = Utils.IsNotNull(dtEmpData)








But, when using extension methods, your code will be

int empID =10;
DataTable dtEmpData= ServiceObject.GetEmpData(10);
bool blnValue = dtEmpData.isNotNull()










This is a cool new way! More readable code of course! All you need is a Static class with a slightly changed method signature. This is how you can achieve

//can be in the same class, project or in a different project
public static class Extensions
{
public static bool isNotNull(this object obj)
{
return obj != null ? true : false;
}
}


Now lets note down the differences

1. These utility methods should be in a static class
2. Method should be static
3. Method has a new parameter “this”. This instructs the compiler to add this method as an extension to the passing object type.
4. As the passing object type is “object”, the current method will be available for all the other classes as “object” is the base class of all the other classes.

Why static class?

Static methods and members (properties) are used when their behaviour is not affected by the state of the containing object. From another point of view we can say that “static methods and members can’t modify the state of an instance”. This is just the reverse of the previous definition, but it has some “thinking” factor associated with it.
A static class will contain only the static methods and properties. (If you declare an instance method, you will get a compilation error). Further, you can’t programme a static class as it is extending from another class. I.e. you can’t modify any of the behaviour of another class using a static class.

But this still doesn’t answer the question. Why a static class? Why can’t we just have it as a static method?

This is the catch. Similar to any other type, Static classes are also loaded by the compiler when they are first referred. But unlike other types a Static object will remain in the memory for the lifetime of the application domain which the current programme resides. [Source: http://msdn.microsoft.com/en-us/library/79b3xss3.aspx] Hence if we use a static class now it is guaranteed that our method definitions will be at memory until the application domain discarded. Now you should understand what would have been happen if we used a static method in some other class rather than using a static class. (Who knows when the garbage collector discards our object?). How silly we were to think about that question, “why static class?”

Now there is another question. Even though we declared the method as static, when we are using it, we have used it similar to an instance method (e.g. dtEmpData.isNotNull() rather than DataTable.isNotNull() ). This magic is done by the C# Intermediate Language compiler.