C Sharp 4.0
Encyclopedia
C# 4.0 is the latest version of the C# programming language, which was released on April 11, 2010. Microsoft
has 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
.
. For example:
Dynamic method calls are triggered by a value of type "dynamic" 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,
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
interfaces and delegates can have their type parameters marked as covariant or contravariant
, using keywords
Therefore, any class that implements
For contravariance, the existing interface
Therefore, any class that implements
the invocation can now be written as either
or
In addition, to complement optional parameters, it is possible to explicitly specify parameter names in method calls, allowing to selectively pass any given 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 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 ref keyword when using COM, can further be shortened as
Microsoft
Microsoft Corporation is an American public multinational corporation headquartered in Redmond, Washington, USA that develops, manufactures, licenses, and supports a wide range of products and services predominantly related to computing through its various product divisions...
has released the 4.0 runtime
Run-time system
A run-time system is a software component designed to support the execution of computer programs written in some computer language...
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
Dynamic Language Runtime
The Dynamic Language Runtime from Microsoft is an ongoing effort to bring a set of services that run on top of the Common Language Runtime and provides language services for several different dynamic languages...
and COM
Component Object Model
Component Object Model is a binary-interface standard for software componentry introduced by Microsoft in 1993. It is used to enable interprocess communication and dynamic object creation in a large range of programming languages...
.
Dynamic member lookup
A new pseudo-typedynamic
is introduced into the C# type system. It is treated as System.Object
, 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 typingDuck typing
In computer programming with object-oriented programming languages, duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface...
. For example:
Dynamic method calls are triggered by a value of type "dynamic" as any implicit or explicit parameter (and not just a receiver). For example:
Dynamic lookup is performed using three distinct mechanisms: COM IDispatch
IDispatch
IDispatch is the interface that exposes the OLE Automation protocol. It is one of the standard interfaces that can be exposed by COM objects. The I in IDispatch refers to interface...
for COM objects,
IDynamicMetaObjectProvider
DLR interface for objects implementing that interface, and reflectionReflection (computer science)
In computer science, reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime....
for all other objects. Any C# class can therefore intercept dynamic calls on its instances by implementing
IDynamicMetaObjectProvider
.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
dynamic
. Values of type dynamic
are implicitly convertible both from and to any other type. In the code sample above, this permits GetLength
function to treat the value returned by a call to Length
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
GenericGeneric programming
In a broad definition, generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters...
interfaces and delegates can have their type parameters marked as covariant or contravariant
Covariance and contravariance (computer science)
Within the type system of a programming language, covariance and contravariance refers to the ordering of types from narrower to wider and their interchangeability or equivalence in certain situations ....
, using keywords
out
and in
, 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 IEnumerable
has been redefined as follows:Therefore, any class that implements
IEnumerable
for some class Derived
is also considered to be compatible with IEnumerable
for all classes and interfaces Base
that Derived
extends, directly, or indirectly. In practice, it makes it possible to write code such as:For contravariance, the existing interface
IComparer
has been redefined as follows:Therefore, any class that implements
IComparer
for some class Base
is also considered to be compatible with IComparer
for all classes and interfaces Derived
that are extended from Base
. It makes it possible to write code such as:Optional ref keyword when using COM
The ref keyword for callers of methods is now optional when calling into methods supplied by COM interfaces. Given a COM method with the signaturethe 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 to explicitly specify parameter names in method calls, allowing to selectively pass any given 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 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 ref keyword when using COM, can further be shortened as