Martin
Martin

Reputation: 24308

c# : Why is a cast needed from an Enum to an INT when used in a switch statement?, enums are ints

Can anyone tell me why i need to cast to Int from my enum

        switch (Convert.ToInt32(uxView.SelectedValue))
        {
            case (int)ViewBy.Client:

If i remove the cast (int) it fails and says i must use a cast.

Here is my enum, and enums are ints.... anybody know about this?

    public enum ViewBy
    {
        Client,
        Customer
    }

Upvotes: 17

Views: 12121

Answers (7)

Anton Gogolev
Anton Gogolev

Reputation: 115749

In C# enums aren't just numbers. Rather, they are numbers associated with the type or number with a name in a context.

To avoid casts in case statements, you can do a cast in switch:

switch((ViewBy)Convert.ToInt32(uxView.SelectedValue))

This, however, has its own problems. For example, this piece of code will write out 7 to the console.

enum ViewBy
{
    ChuckNorris = 1,
    JamesBond
}

Console.WriteLine((ViewBy)7);

Upvotes: 12

Vitalogy
Vitalogy

Reputation: 1

Why would you need a cast to int at all? If uxView.SelectedValue is a ViewBy enum then simply (ViewBy) uxView.SelectedValue would be enough right?

Upvotes: 0

Matt
Matt

Reputation: 2790

In response to the question "Can anyone tell me why i need to cast to Int from my enum"....

You have to cast to an integer because an Enums values are not integers. Enums have an underlying value that can be stored as an integral type (Default is int).

You can determine the underlying type with

Enum.GetUnderlyingType( typeof(ViewBy) );

You can find more details here and here

Upvotes: 0

marcelo-ferraz
marcelo-ferraz

Reputation: 3257

I suppose that in such case looks kind of lame, having the conversion for a int and all.

Supposing that you'd passed an int, and not a enum, to that method, keep in mind:

  1. Are you going to document that well enough so that any time un the future you will be able to fix or enhance the system?
  2. Is this enum not used elsewhere? (A pattern on how to deal with that same information is the key for not making juniors mistakes or losing yourself.)

If the answer the both of the two is no, then you should consider keep it for the sake of your own sanity, in a near future.

And btw, is simpler to convert to int and having it to be compared with another int, than a string, per example.

Upvotes: 0

icecrime
icecrime

Reputation: 76755

From MSDN :

switch ( expression )
      case constant-expression : statement
   [default  : statement]

The constant-expression in each case label is converted to the type of expression and compared with expression for equality. Control passes to the statement whose case constant-expression matches the value of expression.

There is no implicit conversion from enum to int, so each of your case requires an explicit conversion. Note however that initially switching on an expression of the correct type would make more sense than casting every case (see Enum.Parse).

Upvotes: 2

Thomas Weller
Thomas Weller

Reputation: 11717

Enums are NOT ints in the .NET typesystem!
The framework stores them with the aid of discrete integer values - that's a different thing...

Thomas

Upvotes: 2

vgru
vgru

Reputation: 51224

You should convert your string value to an enum using Enum.Parse.

ViewBy selected = (ViewBy) Enum.Parse(typeof(ViewBy), uxView.SelectedValue);        
switch (selected)
{
    case ViewBy.Client:
    // etc.
}

Also, underlying type for an enum doesn't necessarily need to be Int32.

Upvotes: 4

Related Questions