What is the purpose of CS0161 when switching over an enum and how to work around it?

Posted by Danvil on Stack Overflow See other posts from Stack Overflow or by Danvil
Published on 2010-05-09T18:48:43Z Indexed on 2010/05/09 18:58 UTC
Read the original article Hit count: 218

Filed under:

Consider the following example of a simple enum and a switch over all possible enum values.

enum State { Alpha, Beta, Gamma }

State state = ...;

switch(state) {
case Alpha: ... break;
case Beta: ... break;
case Gamma: ... break;
}

Now I get error CS0161: not all code paths return a value.

This seems rather pointless to me, because I would expect that other cases are not possible. So what is the purpose of this error, and how would one work around this in the most elegant way?

© Stack Overflow or respective owner

Related posts about c#