Tuesday, October 7, 2008

New Features of C#.NET 2008 Language (Part 1)

Everbody wondering Microsoft releasing new version of .NET very frequently so it is some what difficult to learn new features. I was thinking to write artical on new features of C#.NET 2008 language. Today I got some time to write so lets starts with summary of new features.

Followings are the new features of C#.NET 2008:



  1. Implicitly Typed Local Variables
  2. Automatic Properties
  3. Partial Methods
  4. Extension Methods
  5. Object Initializer Syntax
  6. Anonymous Types

(1) Implicitly Typed Local Variables:
C# 2008 provides a new keyword var, which can use in place of specifying a formal data type (such as int, string, double). So now you can declare variable as follow:

var myCounter = 15;
var myBool = true;

When you declare variables as above, the compiler will automatically infer the underlying data types based on the initail value used to initialize the variables.
You can use this implicit typing for any type including arrays, generic types and your own custom types.

Limitations of Implicitly Typed Variables:
(1) Implicity typing applies only to local varibles in a method or property scope.
(2)We can not use var keyword to define return values, parameters or field data of a type.

// Error! var cannot be used as field data!
private var myInt = 10;
// Error! var cannot be used as a return value or parameter type
public var MethodName(var a, var b){}

(3)Local variable declared with var keyword must be assigned an initial value at the time of declaration and cannot be assigned the initial value of null.

//Error! Must assign a value
var myVar
// Error! Must assign value at exact time of declaration!
var myVar;
myVar = 0;
// Error! Can't assign null as initial value!
var myObject = null;

(4) It is illegal to define a nullable implicitly typed local variable using the C#? token.

You have seen syntax and limitations of implicitly typed local variables, I am sure you are wondering when to make use of this construct as var keyword is more confusing then int keyword for declaring integer variable. So the real use of implicitly typed local variable is in LINQ.

(2) Automatic Properties:
As a developer, we must know the use of properties. Generally we creates properties to give access of our class or interface to outside world.
C# property syntax is not too problematic but just imagine if you are modeling a class that requires 20 properties then you have to declare 20 private variables (backing fields) for 20 properties which requires lots of typing.
C# 2008 provides solution of this problem by providing feature of Automatic Properties. As the name implies, this feature will offload the work of defining a private backing field and the related C# property member to the compiler using a new bit of syntax.


e.g.
class Customer
{
// Automatic Property Syntax
public string CutomerName {get; set;}
}


When defining automatic properties, you simply specify the access modifier, underlying data type, property name and empty get/set scopes. At compile time, your class will be provided with an autogenerated private backing fields.

One more thing about automatic property, it is not possible to build read-only or write-only automatic properties.


// Read-only property? Error!
public int ReadOnlyProperty { get; }
// Write only property? Error!
public int WriteOnlyProperty { set; }


(3) Partial Methods:
As we know, .NET 2.0 provides facility of partial class which allows us to partition the full implementation of a class across multiple code files.
C# 2008 widens the scope of the partial keyword in that it can now be applied on the method level. This allows you to prototype a method in one file and implementation in another file. However C# partial methods have a number of following restrictions:

• Partial methods must return void.
• Partial methods can not have arguments with the out modifier.
• Partial methods are always implicitly private.
• Partial methods can be static or instance level.
• Partial methods can only be defined within a partial class.

Example:
Partial class A
{
public int sum(int a, int b)
{
varifynumbers(int a, int b);
}
//Partial method
partial void verifynumbers(int a, int b);
}

Implementation of verifynumers method in another file:
partial class A
{
partial void verifynumbers(int a, int b)
{
//some logic here
}
}

Uses of Partial Methods:
From above limitations, it is hard to see many useful applications of this new langauge feature. The truth is partial methods will more likely used feature!!!
Consider verifynumber() method of our example perform some very intensive calculations. By marking this method with the partial modifier, other class builders have the option of providing implementation details if they so choose. In this case, partial methods provide a cleaner solution than using preprocessor directives, supplying “dummy” implementations to virtual methods or throwing NotImplementedException objects.



In some ways, C# partial methods are a strongly typed version of conditional code compilation (via the #if, #elif, #else, and #endif preprocessor directives).

You can find second part of this post at New Features of C#.NET 2008 Language (Part 2).

No comments: