George Mauer
George Mauer

Reputation: 122212

How to get the underlying value of an enum

I have the following enum declared:

 public enum TransactionTypeCode { Shipment = 'S', Receipt = 'R' }

How do I get the value 'S' from a TransactionTypeCode.Shipment or 'R' from TransactionTypeCode.Receipt ?

Simply doing TransactionTypeCode.ToString() gives a string of the Enum name "Shipment" or "Receipt" so it doesn't cut the mustard.

Upvotes: 18

Views: 28940

Answers (7)

J D OConal
J D OConal

Reputation: 624

Marking this as not correct, but I can't delete it.

Try this:

string value = (string)TransactionTypeCode.Shipment;

Upvotes: -3

Tonio
Tonio

Reputation: 22

the underlying values of the enum has to be numeric. If the type of underlying values are known, then a simple cast returns the underlying value for a given instance of the enum.

enum myEnum : byte {Some = 1, SomeMore, Alot, TooMuch};
myEnum HowMuch = myEnum.Alot;
Console.Writeline("How much: {0}", (byte)HowMuch);

OUTPUT: How much: 3

OR (closer to the original question)

enum myFlags:int {None='N',Alittle='A',Some='S',Somemore='M',Alot='L'};
myFlags howMuch = myFlags.Some;
Console.WriteLine("How much: {0}", (char)howMuch);
//If you cast as int you get the ASCII value not the character.

This is recurring question for me, I always forget that a simple cast gets you the value.

Upvotes: 1

nawfal
nawfal

Reputation: 73283

The underlying type of your enum is still int, just that there's an implicit conversion from char to int for some reason. Your enum is equivalent to

TransactionTypeCode { Shipment = 83, Receipt = 82, }

Also note that enum can have any integral type as underlying type except char, probably for some semantic reason. This is not possible:

TransactionTypeCode : char { Shipment = 'S', Receipt = 'R', }

To get the char value back, you can just use a cast.

var value = (char)TransactionTypeCode.Shipment;

// or to make it more explicit:
var value = Convert.ToChar(TransactionTypeCode.Shipment);

The second one causes boxing, and hence should preform worse. So may be slightly better is

var value = Convert.ToChar((int)TransactionTypeCode.Shipment);

but ugly. Given performance/readability trade-off I prefer the first (cast) version..

Upvotes: 1

IaCoder
IaCoder

Reputation: 12770

This is how I generally set up my enums:

public enum TransactionTypeCode {

  Shipment("S"),Receipt ("R");

  private final String val;

  TransactionTypeCode(String val){
    this.val = val;
  }

  public String getTypeCode(){
    return val;
  }
}

System.out.println(TransactionTypeCode.Shipment.getTypeCode());

Upvotes: -1

I was Searching For That and i get the Solution Use the Convert Class

int value = Convert.ToInt32(TransactionTypeCode.Shipment);

see how it easy

Upvotes: 0

Andre
Andre

Reputation: 411

You have to check the underlying type of the enumeration and then convert to a proper type:

public enum SuperTasks : int
    {
        Sleep = 5,
        Walk = 7,
        Run = 9
    }

    private void btnTestEnumWithReflection_Click(object sender, EventArgs e)
    {
        SuperTasks task = SuperTasks.Walk;
        Type underlyingType = Enum.GetUnderlyingType(task.GetType());
        object value = Convert.ChangeType(task, underlyingType); // x will be int
    }    

Upvotes: 41

Andy
Andy

Reputation: 39

I believe Enum.GetValues() is what you're looking for.

Upvotes: 2

Related Questions