Les nouveautés de c# 4.0
4.0
New features in C# 4.0
Mads Torgersen, C# Language PM
Contents
Introduction 1
Dynamic binding 3
Named and Optional Arguments 6
Features for COM interop 8
Variance 9
Relationship with Visual Basic 11
Resources 11
Introduction
The major theme for C# 4.0 is dynamic programming. Increasingly, objects are “dynamic” in the sense that their structure and behavior is not capturedby a static type, or at least not one that the compiler knows about when compiling your program. Some examples include
• objects from dynamic programming languages, such as Python or Ruby
• COM objects accessed through IDispatch
• ordinary .NET types accessed through reflection
• objects with changing structure, such as HTML DOM script objects
• data readers andother user defined dynamic objects
While C# remains a statically typed language, we aim to vastly improve the interaction with such objects.
A secondary theme is co-evolution with Visual Basic. Going forward we will aim to maintain the individual character of each language, but at the same time important new features should be introduced in both languages at the same time. They should bedifferentiated more by style and feel than by feature set.
The new features in C# 4.0 fall into four groups:
Dynamic binding
Dynamic binding allows you to write method, operator and indexer calls, property and field accesses, and even object invocations which bypass the C# static type checking and instead gets resolved at runtime.
Named and optional arguments
Parameters in C# can nowbe specified as optional by providing a default value for them in a member declaration. When the member is invoked, optional arguments can be omitted. Furthermore, any argument can be passed by parameter name instead of position.
COM specific interop features
Dynamic binding as well as named and optional arguments help making programming against COM less painful than today. On top of that,however, we are adding a number of other features that further improve the interop experience specifically with COM.
Variance
It used to be that an IEnumerable wasn’t an IEnumerable. Now it is – C# embraces type safe “co-and contravariance,” and common BCL types are updated to take advantage of that.
Dynamic Binding
Dynamic binding offers a unified approach to invoking thingsdynamically. With dynamic binding, when you have an object in your hand, you do not need to worry about whether it comes from COM, IronPython, the HTML DOM, reflection or elsewhere; you just apply operations to it and leave it to the runtime to figure out what exactly those operations mean for that particular object.
This affords you enormous flexibility, and can greatly simplify your code, but it doescome with a significant drawback: Static typing is not enforced for these operations. 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. Oftentimes this will be no loss, because the object wouldn’t have a static type anyway, in other cases it is a tradeoff between brevity and safety. In order to facilitate this tradeoff,it is a design goal of C# to allow you to opt in or opt out of dynamic behavior on every single call.
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);
The C# compiler allows you to call a method with any name and any argumentson d because it is of type dynamic. At runtime the actual object that d refers to will be examined to determine what it means to “call M with an int” on it.
The type dynamic can be thought of as a special version of the type object, which signals that the object can be used dynamically. It is easy to opt in or out of dynamic behavior: any object can be implicitly converted to dynamic,…