TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
C# Coding Standard

Rule:  7@304Checked automatically with code checker

This rule is Obsolete
Synopsis:Only use optional arguments to replace overloads
Severity Level:3
Category:Object oriented


The only valid reason for using C# 4.0's optional arguments is to adhere to rule [7@303] with a single method like:

public virtual int IndexOf(string phrase, int startIndex = 0, int count = 0)
    return someText.IndexOf(phrase, startIndex, count);

If the optional parameter is a reference type then it can only have a default value of null. But since strings, lists and collections should never be null according to rule [5@117], you must use overloaded methods instead.

Note The default values of the optional parameters are stored at the caller side. As such, changing the default value without recompiling the calling code will not apply the new default value properly.

Note When an interface method defines an optional parameter, its default value is not considered during overload resolution unless you call the concrete class through the interface reference.

Literature References:
Aviva AV1553