Make sure your enum
has a default value (equal to zero), that means "none" or "invalid". This would be an appropriate value for your optional parameter's default value.
This is recommended by Microsoft Code Analysis as well, CA1008: Enums should have zero value.
For example:
enum SpeakerType
{
None = 0,
Woofer,
Midrange
Tweeter
}
This way the default
keyword provides a value that is sane, but doesn't unintentionally refer to something you don't want it to.
As an example, the BCL uses this same concept. The number of stop bits to use for a SerialPort
is defined by the StopBits
enum:
public enum StopBits
{
None,
One,
Two,
OnePointFive,
}
However the None
value is invalid. In fact,
The
SerialPort
class throws anArgumentOutOfRangeException
exception when you set theStopBits
property toNone
.