Prash's Blog

New C# Language Features in .Net 4.0 May 7, 2010

Filed under: C# — prazjain @ 11:31 pm
Tags: , , , ,

.Net 4.0 has added couple of new language features in C#

Dynamic Lookup

C# has added a new static type named ‘dynamic’. It supports late binding, so any calls to methods, properties, fields are not resolved at compile time. Thus an error arising due to its use is also detected at runtime.


dynamic person = GetPerson();

// A runtime error would be generated if the object returned by GetPerson() method does not has Name property

person.Name="Prashant";

‘dynamic’ type provides a lot of help to programmers dealing with COM objects in C#

Optional Parameters

C# now provides the ability to assign default values to method parameters incase the caller does not intend to pass value for a parameter.


void Print(string car="Mini Cooper", int price=15000)

{      Console.Writeln(string.format("Car : {0}, Price : {1}",car, price));  }

void M1()

{   Print("VW Golf");  }

Upon invoking M1(), it would print : “Car : VW Golf, Price : 15000”

This shows the parameter ‘price’ assumed the default value.

Named Parameter

Method parameters can now be assigned names, so its no longer necessary that method arguments are passed in the same order as method parameters.


void Print(string car="Mini Cooper", int price=15000)

{      Console.Writeln(string.format("Car : {0}, Price : {1}",car, price));  }

void M1()

{   Print(price: 16000, car: "VW Golf");  }

Upon invoking M1(), it would print : “Car : VW Golf, Price : 16000”

Covariance

Covariance means that an object can be treated as less derived. This features is used to return values from methods that are more abstract.


class Vehicle { ... }

class Car : Vehicle { ... }

class Test {

static void Main(string[] args)

{

...

IEnumerable<Car> cars = GetCars();

// this is now possible in .Net 4.0

IEnumerable<Vehicle> vehicles = cars;

// prior to .Net 4.0 it would have required going through 'cars' collection and add every object into a new collection vehicles

}

}

To allow Covariance the IEnumerable type is defined with an ‘out’ keyword


IEnumerable<out T> { ... }

‘out’ keyword says that its fine to take values out from the collection, adding values is not allowed.

Contravariance

Contravariance allows a more derived type argument to be passed into a less derived type method parameter.


class Vehicle { ... }

class Car : Vehicle { ... }

class Test

{

// Contravariance is enabled by using 'in' keyword

delegate void MyHandler<in T> (T a);

static void Main(string[] args)

{

MyHandler<Vehicle> handler = (veh) => { Console.Writeln(veh); }

// below line is now allowed in .Net 4.0

MyHandler<Car> carHandler = handler;

}

}

All that it means is contravariance allows a Car (subtype) to be passed into a method that requires Vehicle (base type), which could do not harm to the execution of method. But this is only allowed when the delegate generic definition uses ‘in’ keyword, without ‘in’ it would fail in .Net 4.0 too!

Events

Events in C# look like fields / properties. Operators += and -= are overloaded to map to add and remove accessor methods.

Prior to C# 4.0, these generated accessors were synchronized across threads using ‘lock(this)’ as the auto generated code decorated accessors with MethodImpl(MethodImpl.Synchronized) attribute, that meant instead of synchronizing access on that invoke ‘add’ & ‘remove’ method, it would synchronized access across any thread that capture lock on ‘this’ object.

To get across this problem, C# 4.0 changes the way the code is generated for these accessors, it generates code to test for race conditions and updates delegate list accordingly.

This should be a non-breaking changes unless your code is dependent on the way synchronization was implemented earlier.

Advertisements
 

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