Enum base-type question

T

Tyler

The MSDN help on the enum keyword for the C# language indicates that it is
possible to give an enumeration a base-type other than int. In my case, I
have chosen to give it a base type of uint.

The annoying thing I have encountered, is that when I switch on a uint
value, I still have to explicitly cast my enum values to uint - even though
they should be uint values. Is this by design (I don't know why because my
enum is uint and so is the switch value), or is there something I have
missed that would remove the required explicit uint cast?

Thanks, Tyler

Code sample:
------------
class Class1
{
protected enum eTest : uint
{
value1,
value2
};

[STAThread]
static void Main(string[] args)
{
uint tvuiValue = 1;
switch( tvuiValue )
{
case eTest.value1: // ERROR here
break;
case (uint)eTest.value2: // No error here
break;
default:
break;
}
}
}

Errors:
 
M

Mountain Bikn' Guy

I don't believe you have missed anything. I don't believe there is anything
that would allow you to avoid casting.
Regards,
Mountain
 
J

Jeffrey Tan[MSFT]

Hi Tyler,

Thanks for posting in this group.
C# is a strong type language, it is strict on types.
The swith case state in C# does not apply the inheritance comparasion, but
apply equal comparasion.
So you should explicit cast them into one type.

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
T

Tyler

That helps to clarify that I'm not missing something and that the explicit
cast is required in this case.

Thanks, Tyler
 

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