Optional and Named Parameters in C# Programming

Introduction

Welcome to this installment of the .NET Nuts & Bolts column! This time around we’ll explore optional and named parameters in C#. This is an option that has long been around within the Visual Basic language, but is newly introduced in to the C# language in the .NET Framework 4.0.

Why Now?

The C# programming language has been around for 10+ years now depending upon whether you start the timer for its existence with the official release or include the beta period. For those with a Visual Basic history that pre-dates .NET you recognize optional and named parameters. If you’re like myself and started out in the .NET framework with a transition to C# it’s likely that you missed them for a while, but grew to appreciate overloading and having a number of methods with the same name with different signatures. Then eventually we forgot all about it after having gotten used to not having it in C#. Well, it’s time to get reacquainted because optional and named parameters are now part of .

Let’s first take a quick detour in to why they are finally in the C# language after all this time. The object oriented purists were happy to not have them. It’s likely that they may not have made an appearance, but for a couple of key reasons. Reason number one is the new approach of the Microsoft language teams to have parity across the languages. Features will be released in VB.NET and C# at the same time as well as steps are being taken to close some of the gaps between the two so that there is a more consistent experience regardless of the language. Reason number two is the improved COM interoperability that was part of the objectives of the .NET Framework 4.0 release. Well rooted items such as the Microsoft Office COM support optional and named parameters rather than providing numerous method signatures with different parameters. The office API is firmly grounded and less likely to undertake a major overhaul. It is far simpler to add optional and named parameter support to C#.


// Original bloated C# code to Save a Word document
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);
 
// Replacement slimmed down code now possible due to optional params
doc.SaveAs(“Test.docx”);

Optional Parameters in C# Programming

C# has classically used overloading in order to provide alternate method signatures. It commonly involves having a primary declaration will a complete parameter list, and additional overloads with a shortened parameter list. Commonly, the shorter declarations just call the primary with desired default values. This offers the behavior of optional parameters, but does require more code. The following code example demonstrates the concept.


// Primary declaration
public StreamReader OpenTextFile(
string path,
Encoding encoding = null,
bool detectEncoding = true,
int bufferSize = 1024)
{
// Relevant method implementation here…
}

// Example calls leaving off parameters
OpenTextFile(“foo1.txt”, Encoding.UTF8, true, 2048);
OpenTextFile(“foo2.txt”, Encoding.UTF8, false);
OpenTextFile(“foo3.txt”, Encoding.UTF8);
OpenTextFile(“foo4.txt”);


More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read