Casting an Enum to Invalid Value

O

O.B.

Why doesn't the cast to "4" in the statement below not throw an
exception?


private enum TestEnum
{
Zero,
One,
Two,
Three
}

[Test]
public void WeirdEnumBehavior()
{
TestEnum e = TestEnum.One;
Console.WriteLine(e.ToString()); // Prints "One"

e = (TestEnum) 4; // Why doesn't this throw an exception?
Console.WriteLine(e.ToString()); // Prints "4"
}
 
P

Peter Duniho

Why doesn't the cast to "4" in the statement below not throw an
exception?

Because the C# specification specially allows for undeclared enum values
to be stored in an enum type variable.

If you want to know why the C# language designers picked that particular
behavior, that's something you'd have to ask them. But I suspect it's
simply a matter of simplicity and performance (the overhead of validating
values would be significant...huge, even, compared to simply assigning a
value) versus whatever marginal advantage there might be in protecting
against undeclared values being used.

There are also some practical benefits in the context of mixing managed
code with unmanaged (something that comes up in .NET frequently), in that
a managed library doesn't have to anticipate each and every possible value
that might be required; it can provide an enum for convenience, but client
code can still cast an undeclared value to the enum if it has to.

Pete
 
J

Jeff Johnson

Why doesn't the cast to "4" in the statement below not throw an
exception?

Because enums are just very, very fancy numbers and range checking was
purposely not included. I like this, personally, because I often use -1 as
an invalid value marker. May not be the best idea, but it works well for me.
 
T

Tom Spink

Peter said:
If you want to know why the C# language designers picked that particular
behavior, that's something you'd have to ask them. But I suspect it's
simply a matter of simplicity and performance (the overhead of validating
values would be significant...huge, even, compared to simply assigning a
value) versus whatever marginal advantage there might be in protecting
against undeclared values being used.

Hi Pete,

I suspect, also, that you get things like 'Flags' for free.

-- Tom
 
P

Peter Duniho

I suspect, also, that you get things like 'Flags' for free.

Well, they easily could have stipulated that enums with the [Flags] were
exempt from run-time value-checking. But certainly the fact that they
would have had to do that, introducing an arbitrary inconsistency into the
language, may well be yet another argument in favor of the current
behavior.

Pete
 
T

Tom Spink

Peter said:
I suspect, also, that you get things like 'Flags' for free.

Well, they easily could have stipulated that enums with the [Flags] were
exempt from run-time value-checking. But certainly the fact that they
would have had to do that, introducing an arbitrary inconsistency into the
language, may well be yet another argument in favor of the current
behavior.

Pete

Very true. Especially with the amount of run-time sugar that goes on
anyway with the inclusion of [Flags].

-- Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top