Skip to content

4.0

C# 4.0, released with .NET Framework 4 in April 2010, introduced several new features aimed at improving interoperation with dynamic languages and COM APIs, and making the language more flexible. Here are some of the key features introduced in C# 4.0, along with examples and comparisons to earlier versions of C#:

1. Dynamic Binding

The dynamic keyword was introduced to bypass static type checking. This is particularly useful when interacting with COM objects, dynamic languages, or reflection, where types might not be known at compile time.

C# 4.0

dynamic dynamicObject = 4;
dynamicObject = "Hello, world!";  // No compile-time type checking
Console.WriteLine(dynamicObject);  // Output: Hello, world!

C# < 4.0 In earlier versions of C#, you had to use object and often needed explicit casting or reflection to work with types that could change at runtime, which was less convenient and more error-prone.

object obj = 4;
obj = "Hello, world!";
Console.WriteLine((string)obj);  // Requires explicit cast

2. Named and Optional Arguments

Named and optional arguments make method calls more flexible and readable by allowing arguments to be specified by name and by omitting optional arguments.

C# 4.0

public void SendMessage(string to, string subject, string body, bool ccAdmin = false) { }

// Using named and optional arguments
SendMessage(subject: "Hello", body: "World", to: "example@example.com");

C# < 4.0 Previously, you had to provide all arguments in the exact order they were defined in the method signature, and use method overloads to achieve similar functionality.

public void SendMessage(string to, string subject, string body) { }
public void SendMessageWithCC(string to, string subject, string body, bool ccAdmin) { }

// Without named and optional arguments
SendMessage("example@example.com", "Hello", "World");
SendMessageWithCC("example@example.com", "Hello", "World", false);

3. Generic Covariance and Contravariance

Support for covariance and contravariance in generic interfaces and delegates makes it easier to work with types in a type-safe manner, especially when dealing with inheritance hierarchies.

C# 4.0

IEnumerable<string> strings = new List<string>();
IEnumerable<object> objects = strings;  // Covariance: IEnumerable<T> is covariant

C# < 4.0 In earlier versions, you could not assign an instance of a generic collection of a derived type to a generic collection of a base type without explicit casting or conversion, even if it was logically safe to do so.

List<string> strings = new List<string>();
// Not allowed in C# 3.0 and earlier: List<object> objects = strings;

4. COM Interop Improvements

C# 4.0 introduced improvements to COM interop, such as the ability to omit ref modifiers on COM method calls, making it easier to work with COM objects.

C# 4.0

// Assume 'comObject' is a COM object with a method 'MethodTakingRefParameter(ref int)'
dynamic comObject = GetComObject();
int value = 10;
comObject.MethodTakingRefParameter(value);  // 'ref' not needed

C# < 4.0 In earlier versions, working with COM objects required more boilerplate code, including the use of ref and out modifiers when calling methods with reference parameters.

// Using early binding and COM interop in C# 3.0
int value = 10;
comObject.MethodTakingRefParameter(ref value);  // 'ref' needed