Wednesday, October 21, 2009

New features in C# 4.0

Introduction

As we all know,Microsoft is coming to release the new version of Visual Studio and .NET framework - Visual Studio 2010 with .NET framework 4.0.
If you're interested to get an early look of C# 4.0 upcoming features, this post is for you.
Generally, there are four main additions introduced in the new version:

1.Dynamic Lookup

2.Named and Optional Arguments

3.Improved COM Interoperability

4. Variance and Contravariance


We will take a look at first two features: Dynamic Lookup & Named and Optional Arguments

Dynamic Lookup

Dynamic lookup allows you to write method, operator and indexer calls, property and field accesses, and even object invocations bypass the C# static type checking and instead gets resolved at runtime.
With dynamic lookup, when you have an object in your hand you do not need to worry about whether it comes from COM, IronPython, the HTML DOM or reflection.
A dynamic object is assumed at compile time to support any operation, and only at runtime will you get an error if it wasn’t so.

The dynamic type

C# 4.0 introduces a new static type called dynamic.

When you have an object of type dynamic you can “do things to it” that are resolved only at runtime:

dynamic d = GetDynamicObject(…);

d.M(7);

Dynamic operations

Not only method calls, but also field and property accesses, indexer and operator calls and even delegate invocations can be dispatched dynamically:

dynamic d = GetDynamicObject(…);
d.M(7); // calling methods
d.f = d.P; // getting and settings fields and properties
d[“one”] = d[“two”]; // getting and setting thorugh indexers
int i = d + 3; // calling operators
string s = d(5,7); // invoking as a delegate

Named and Optional Arguments


Named and optional parameters are really two distinct features, but are often useful together. Optional parameters allow you to omit arguments to member invocations, whereas named arguments is a way to provide an argument using the name of the corresponding parameter instead of relying on its position in the parameter list.

Optional parameters

A parameter is declared optional simply by providing a default value for it:
public void M(int x, int y = 5, int z = 7);
Here y and z are optional parameters and can be omitted in calls:
M(1, 2, 3); // ordinary call of M
M(1, 2); // omitting z – equivalent to M(1, 2, 7)
M(1); // omitting both y and z – equivalent to M(1, 5, 7)

Named arguments

C# 4.0 does not permit you to omit arguments between commas as in M(1,,3). This could lead to highly unreadable comma-counting code. Instead any argument can be passed by name. Thus if you want to omit only y from a call of M you can write:
M(1, z: 3); // passing z by name
or
M(x: 1, z: 3); // passing both x and z by name
or even
M(z: 3, x: 1); // reversing the order of arguments

Overload resolution

Named and optional arguments affect overload resolution, but the changes are relatively simple:
A signature is applicable if all its parameters are either optional or have exactly one corresponding argument (by name or position) in the call which is convertible to the parameter type.

You can find the detailed description of the rest of the features in this document.


Mark.

1 comment: