C Sharp 4.0

1

C# 4.0 is a version of the C# programming language that was released on April 11, 2010. Microsoft released the 4.0 runtime and development environment Visual Studio 2010. The major focus of C# 4.0 is interoperability with partially or fully dynamically typed languages and frameworks, such as the Dynamic Language Runtime and COM.

Features

The following new features were added in C# 4.0.

Dynamic member lookup

A new pseudo-type is introduced into the C# type system. It is treated as, but in addition, any member access (method call, field, property, or indexer access, or a delegate invocation) or application of an operator on a value of such type is permitted without any type checking, and its resolution is postponed until run-time. This is known as duck typing. For example: Dynamic method calls are triggered by a value of type as any implicit or explicit parameter (and not just a receiver). For example: Dynamic lookup is performed using three distinct mechanisms: COM IDispatch for COM objects, DLR interface for objects implementing that interface, and reflection for all other objects. Any C# class can therefore intercept dynamic calls on its instances by implementing. In case of dynamic method and indexer calls, overload resolution happens at run-time according to the actual types of the values passed as arguments, but otherwise according to the usual C# overloading resolution rules. Furthermore, in cases where the receiver in a dynamic call is not itself dynamic, run-time overload resolution will only consider the methods that are exposed on the declared compile-time type of the receiver. For example: Any value returned from a dynamic member access is itself of type. Values of type are implicitly convertible both from and to any other type. In the code sample above this permits function to treat the value returned by a call to as an integer without any explicit cast. At run time the actual value will be converted to the requested type.

Covariant and contravariant generic type parameters

Generic interfaces and delegates can have their type parameters marked as covariant or contravariant using keywords and respectively. These declarations are then respected for type conversions, both implicit and explicit, and both compile time and run time. For example, the existing interface has been redefined as follows: Therefore, any class that implements for some class is also considered to be compatible with for all classes and interfaces that extends, directly or indirectly. In practice, it makes it possible to write code such as: For contravariance, the existing interface has been redefined as follows: Therefore, any class that implements for some class is also considered to be compatible with for all classes and interfaces that are extended from. It makes it possible to write code such as:

Optional ref keyword when using COM

The keyword for callers of methods is now optional when calling into methods supplied by COM interfaces. Given a COM method with the signature the invocation can now be written as either or

Optional parameters and named arguments

C# 4.0 introduces optional parameters with default values as seen in Visual Basic and C++. For example: In addition, to complement optional parameters, it is possible explicitly to specify parameter names in method calls, allowing the programmer selectively to pass any subset of optional parameters for a method. The only restriction is that named parameters must be placed after the unnamed parameters. Parameter names can be specified for both optional and required parameters, and can be used to improve readability or arbitrarily to reorder arguments in a call. For example: Optional parameters make interoperating with COM easier. Previously, C# had to pass in every parameter in the method of the COM component, even those that are optional. For example: With support for optional parameters, the code can be shortened as Which, due to the now optional keyword when using COM, can further be shortened as

Indexed properties

Indexed properties (and default properties) of COM objects are now recognized, but C# objects still do not support them.

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

Edit article