C# 4.0 Features
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 or late binding. For example:
In this case, dynamic type is used to avoid more verbose Reflection. It still uses Reflection under the hood, but it's usually faster thanks to caching.
This feature is primarily targeted at interoperability with dynamic languages.
Dynamic type has applications even in mostly statically typed code, for example it makes double dispatch posible without implementing Visitor pattern.
Optional parameters and named arguments
We can omit the argument in the call if that argument is an Optional Argument Every Optional Argument has its own default value It will take default value if we do not supply the value A default value of a Optional Argument must be a
- Constant expression.
- Must be a value type such as enum or struct.
- Must be an expression of the form default(valueType)
It must be set at the end of parameter list
Method parameters with default values:
As said by MSDN, A named argument ,
Enables you to pass the argument to the function by associating the parameter’s name No needs for remembering the parameters position that we are not aware of always. No need to look the order of the parameters in the parameters list of called function. We can specify parameter for each arguments by its name.
Limitation of using a Named Argument
Named argument specification must appear after all fixed arguments have been specified.
If you use a named argument before a fixed argument you will get a compile time error as follows.
Named argument specification must appear after all fixed arguments have been specified
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
the invocation can now be written as either
Generic interfaces and delegates can have their type parameters marked as covariant or contravariant using the
in keywords 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<T> has been redefined as being covariant:
The existing interface IComparer has been redefined as being contravariant: