enum Answer : int { Yes = 1, No = 2 }
Answer answer = (Answer)100;
It throws, but I don't want it to. How?
enum Answer : int { Yes = 1, No = 2 }
Answer answer = (Answer)100;
It throws, but I don't want it to. How?
No, it does not throw. Your typecast line does exactly what you want.
enums are purposely relaxed about accepting 'invalid' values, this is necessary when using bit-flags.
This goes against the concept of enums... They exist to ensure that the option exists. If you need 100 different values my advice is to loose the enum. If you just need "another value" you could do this:
enum Answer : int { Yes = 1, No = 2, OtherValue }
Answer answer = Answer.OtherValue;
Why would you want to do this? Why not simply use an int if you are not using the features of the enum?
That said, the code does not pose any problem for me:
Answer answer = (Answer)100;
Console.WriteLine(answer); // prints 100
Huh, it throws? Are you sure you are using C#? This code is perfectly valid and does what you want it to do!