30

I've seen posts relating to C++, but am asking specifically for C# .NET (4.0+).

In the following example is a default case necessary?

public enum MyEnum : int
{
    First,
    Second
}

public class MyClass
{

    public void MyMethod(MyEnum myEnum)
    {
        switch (myEnum)
        {
            case MyEnum.First: /* ... */ break;
            case MyEnum.Second: /* ... */ break;

            default: /* IS THIS NECESSARY??? */ break;
        }
    }
}
Community
  • 1
  • 1
mirezus
  • 13,892
  • 11
  • 37
  • 42
  • 1
    Possible duplicate of [Should switch statements always contain a default clause?](https://stackoverflow.com/questions/4649423/should-switch-statements-always-contain-a-default-clause) – Rekshino Feb 20 '18 at 10:30

7 Answers7

49

It's a common misconception that .Net enum values are limited to the ones declared in the Enum. In reality though they can be any value in the range of the base type of the enum (int by default). For example the following is perfectly legal

MyMethod((MyEnum)42);

This code will compile without warnings and hit none of your case labels.

Now whether your code chooses to handle this type of scenario is a policy decision. It's not necessary but I'd certainly recomend having one. I prefer to add a default to every switch on enum I write specifically for this scenario with the following pattern

switch (value) { 
  ...
  default: 
    Debug.Fail(String.Format("Illegal enum value {0}", value));
    FailFast();  // Evil value, fail quickly 
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 7
    I would add to Jared's answer that if you have an enum based on a byte, and you have *all 256 cases* in your switch, even then the compiler still might want a default case for definite assignment checking purposes. See http://blogs.msdn.com/b/ericlippert/archive/2009/08/13/four-switch-oddities.aspx for details. – Eric Lippert Dec 17 '10 at 18:36
  • 1
    Another case where this comes into play is if the enum is defined in a different assembly. In that case, someone might add a field to the enum and then run your code against the new version without recompiling. The compiler insists on generating code that is robust to this kind of thing. – ChaseMedallion Apr 20 '17 at 13:21
14

It's not strictly necessary, but someone may pass in a value not covered by your enum (since enumerations do not actually restrict the range of permissible parameter values).

I typically add a default and throw if the specified value is unexpected.

Jeff Sternal
  • 47,787
  • 8
  • 93
  • 120
8

It is not technically necessary, but because you can easily cast a value of MyEnums underlying type (usually int) to an instance of MyEnum. Hence it is good practice to add a default statement with a Debug.Assert() in it.

Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
5

It is not required but good practise as someone might introduce a new enumeration later on. For example throw an exception that indicates that 'unknown' enumeration is not handled.

Philip Fourie
  • 111,587
  • 10
  • 63
  • 83
3

No, the default case is not required.

Matt Kellogg
  • 1,234
  • 8
  • 16
0

From a purely code perspective, there's no requirement to have a default case. It's solely a matter of your logical requirements.

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
0

Its needed if you cast into enum undefined value (will not throw an exception), also if you add more values to enum.