enum is int

J

jonpb

The documentation for the "is" keyword says:

An is expression evaluates to true if the provided expression is
non-null, and the provided object can be cast to the provided type
without causing an exception to be thrown.

So, given:

enum TestEnum { t1, t2 }
TestEnum te = TestEnum.t1;
int ti = (int)te;

if (te is int)
{
}

Why does the test for (te is int) fail? Is there a work around for a
function like this:

string CStr(Object arg)
{
if (arg is int)
{
}
}

other then always casting enums to ints?

Thanks
 
J

jonpb

On second thought, I guess it requires an implicit cast and Enum (BCL)
only defines an explicit one? In terms of a work around for the function:

if (te is Enum)
{
}

works for my purposes.

Sorry for the bandwidth, but I hope this might help someone else.
 
M

MichaelC

jonpb said:
On second thought, I guess it requires an implicit cast and Enum (BCL)
only defines an explicit one? In terms of a work around for the function:

if (te is Enum)
{
}

works for my purposes.

Sorry for the bandwidth, but I hope this might help someone else.

Remember an enum is not always an int.

Michael
 

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