BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Optional Parameters Are Gaining Ground in .NET

Optional Parameters Are Gaining Ground in .NET

Leia em Português

This item in japanese

Optional parameters have always been part of .NET, but with C# unwilling to support it, using them was generally considered taboo unless work with COM libraries. Now that C# 4 does support them, we are starting to see them used for a lot more than just legacy code. Other uses include interoperability with dynamic languages, immutable data structures, and various parts of ASP.NET MVC.

COM is of course still the primary use for optional parameters in .NET. When dealing with libraries such as Office which can have 20+ parameters on function, manually specifying every argument is incredibly tedious. And without labels, one is left to the error prone process of counting slots to determine which argument is mapped to which parameter. For those unfamiliar with COM, it was created back in the days when not all of the popular languages supported function overloading. Since functions containing optional parameters could always be treated as normal functions, this made a lot of sense at the time.

In addition to the complaints of COM programmers, the C# team was using C# 4 as a chance to introduce late binding so they could support the DLR. In fact, that was the only feature they actually wanted to support, everything else just came along for the ride. Optional parameters are needed in dynamic languages, as the lack of explicit typing means they can’t overload functions based on parameter types.

With the increased availability of multi-core and multi-CPU machines, discussions about parallelism and concurrency are becoming more common. A common topic is the usefulness of immutable data structures and the problems with defining them. If every property needs to be set then a standard constructor makes sense. But if most properties are optional, things become somewhat tricky. Overloaded constructors only work to a certain point, after which people using Java or older versions of C# they would often turn to builder patterns. But with optional parameters one can easily do the job with a single constructor.

Controllers in ASP.NET MVC 2 now support default values for query string parameters. While they can be specified using attributes, the most succinct way to indicate them is to use the optional parameter syntax in C# or VB.

Microsoft’s Razor view engine for ASP.NET MVC uses optional parameters for HTML helpers. This allows them to add a lot of options to the helper while providing a self-documenting coding style. Razor does take things one step further, as it also supports automatically turning expressions into delegates without the lambda or anonymous delegate syntax.

There are some places where we expect optional parameters to never be found. The Common Language Specification, which defines the subset of the CLR that all languages should support, explicitly disallows a reliance on optional parameters. This means they are not a candidate for use in the Base Class Library and will probably never been seen in any of the other libraries shipped as part of the .NET Framework. But for the out-of-band libraries released via CodePlex the rules are considerably laxer.

Rate this Article

Adoption
Style

BT