C Sharp 4.0
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.[1] 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 have been added to C# 4.0.[2]
Dynamic member lookup
A new pseudo-type dynamic
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 typing.[citation needed] For example:
// Returns the value of Length property or field of any object
int GetLength(dynamic obj)
{
return obj.Length;
}
GetLength("Hello, world"); // a string has a Length property,
GetLength(new int[] { 1, 2, 3 }); // and so does an array,
GetLength(42); // but not an integer - an exception will be thrown here at run-time
}
</source>
Therefore, any class that implements IComparer<Base>
for some class Base
is also considered to be compatible with IComparer<Derived>
for all classes and interfaces Derived
that are extended from Base
. It makes it possible to write code such as:
IComparer<object> objectComparer = GetComparer();
IComparer<string> stringComparer = objectComparer;
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 signature
void Increment(ref int x);
the invocation can now be written as either
Increment(0); // no need for "ref" or a place holder variable any more
or
int x = 0;
Increment(ref x);
Optional parameters and named arguments
C# 4.0 introduces optional parameters with default values as seen in Visual Basic and C++. For example:
void Increment(ref int x, int dx = 1)
{
x += dx;
}
int x = 0;
Increment(ref x); // dx takes the default value of 1
Increment(ref x, 2); // dx takes the value 2
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:
Stream OpenFile(string name, FileMode mode = FileMode.Open, FileAccess access = FileAccess.Read) { ... }
OpenFile("file.txt"); // use default values for both "mode" and "access"
OpenFile("file.txt", mode: FileMode.Create); // use default value for "access"
OpenFile("file.txt", access: FileAccess.Read); // use default value for "mode"
OpenFile(name: "file.txt", access: FileAccess.Read, mode: FileMode.Create); // name all parameters for extra readability,
// and use order different from method declaration
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:
object fileName = "Test.docx";
object missing = System.Reflection.Missing.Value;
doc.SaveAs(ref fileName,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
With support for optional parameters, the code can be shortened as
doc.SaveAs(ref fileName);
Which, due to the now optional ref keyword when using COM, can further be shortened as
doc.SaveAs(fileName);
Indexed properties
Indexed properties (and default properties) of COM objects are now recognized, but C# objects still do not support them.
References
- ^ "Microsoft Visual Studio 2010 First Look".
- ^ Torgersen, Mads (2008-10-27). "New features in C# 4.0". Microsoft. Retrieved 2008-10-28.
External links
- C# Future (Microsoft MSDN) with link to "New Features in C# 4.0" document
- C# 4.0 Language Specification
{{{1}}}