Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News C# Feature Focus: Optional and Named Parameters, COM Interoperability

C# Feature Focus: Optional and Named Parameters, COM Interoperability

This item in japanese

Due to overwhelming customer demand, Anders has "relented" and allowed Optional and Named Parameters in C#. Just like VB, optional parameters are supported by including a default value. And like in VB, required parameters must all appear before any optional parameter.

Named parameters are also supported fully. This allows developers to specify the parameters in any order they see fit. This works with normal as well as optional parameters, one would expect to only see it when dealing with the latter.

When resolving expressions used as arguments, the order is strictly determined by the code making the call irregardless of the order in the function's signature. And though not recommended, this means expressions with side effects can be more or less safely used as arguments.

COM libraries have a bad habit of returning values as type Object. To address this, C# 4 will automatically promote any object returned by COM to the "dynamic" type. As we mentioned in the previous article, this allows late-bound calls on the object just as if the developer had manually written all the necessary reflection code.

The "ref" modifier will no longer be needed for COM calls unless by reference semantics are actually needed.

In the end this means code that used to look like this:

//C# 3
var a = (IFoo) obj.Foo(ref missing, ref missing, 
                       ref missing, ref missing, 
                       ref missing, 5, ref missing, 
                       ref missing, ref missing);
var b = (IBar)a.Bar();
var c = b.Value;

//C# 4
dynamic obj = //some COM or DLR object
var c = obj.Foo(clientId := 5).Bar().Value

There was also a rumor about parameterized properties. We will post an update once we have more information on it.

Rate this Article