Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C# 4.0

C# 4.0: Alternative To Optional Arguments

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
17 Apr 2010CPOL 16K   4   4
C# 4.0: Alternative To Optional Arguments
free hit counters

Like I mentioned in my last post, exposing public methods with optional arguments is a bad practice (that’s why C# has resisted having it, until now).

You might argue that your method or constructor has too many variants and having ten or more overloads is a maintenance nightmare, and you're right. But the solution has been there for ages: have an arguments class.

The arguments class pattern in the .NET Framework is used by several classes, like XmlReader and XmlWriter that use such patterns in their Create methods, since version 2.0:

C#
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Auto;
XmlReader.Create("file.xml", settings);

With this pattern, you don't have to maintain a long list of overloads and any default values for properties of XmlReaderSettings (or XmlWriterSettings for XmlWriter.Create) can be changed or new properties added in future implementations that won't break existing compiled code.

You might now argue that it’s too much code to write, but, with object initializers added in C# 3.0, the same code can be written like this:

C#
XmlReader.Create("file.xml", new XmlReaderSettings 
	{ ValidationType = ValidationType.Auto });

Looks almost like named and optional arguments, doesn't it? And, who knows, in a future version of C#, it might even look like this:

C#
XmlReader.Create("file.xml", new { ValidationType = ValidationType.Auto });
This article was originally posted at http://feeds.paulomorgado.net/paulomorgado/blogs/en

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paulo Morgado
Portugal Portugal

Comments and Discussions

 
QuestionIs this the same as optional arguments? Pin
MartinFister18-Apr-10 5:28
MartinFister18-Apr-10 5:28 
AnswerRe: Is this the same as optional arguments? Pin
Paulo Morgado18-Apr-10 5:56
professionalPaulo Morgado18-Apr-10 5:56 
In JavaScript, if you omit the arguments, the function will receive something with a type "undefined".

In C# that's not what will happen with optional arguments. with optional arguments, the default parameter value will be passed.

Because the default parameter value will be hardcoded in the caller code, if you (as de developr of the called code) need to change the default value of the parameter, that won'r reflect in any already compiled code, like it would if method overload was being used.

That's why optional arguments is not recommended whenever you have no control over the calling code.

As an alternative, a arguments class is recommended.
Cheers,

Paulo Morgado

Portugal - Europe's West Coast

GeneralRe: Is this the same as optional arguments? Pin
MartinFister18-Apr-10 7:48
MartinFister18-Apr-10 7:48 
GeneralRe: Is this the same as optional arguments? Pin
Paulo Morgado18-Apr-10 8:28
professionalPaulo Morgado18-Apr-10 8:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.