c sharp

How to Get int value from enum in C#

Enumerations (enums) are a great method for defining a set of values named as constants in C#. It provides a way to define a fixed set of values that can be used in your program. Sometimes, you may need to get an integer value from an enum in C# and this can be useful when you need to store an enum value as an integer, or when you need to perform arithmetic operations on the enum value. In this article, we will discuss how to get an int value from an enum in C# with a code example.

Getting an int value from an enum in C#

Enums are a great way to define a set of related named constants in C# but sometimes we need to get the integer value of an enum and there are different ways to achieve this:

1: How to Get Int Value from Enum in C# Using Object.GetHashCode()

Another way to get the integer value of an enum is to use the GetHashCode() method and here is an example C# code:

using System;

enum cars
{
    BMW,
  Mercedes,
   Audi,
   GMC
}
class Program
{
    static void Main(string[] args)
    {
        cars c = cars.Audi;
        int value = c.GetHashCode();
        Console.WriteLine("The integer value of {0} is {1}", c, value);
    }
}

 
The cars enumeration is defined in the same way as before, with some of the best cars companies. The Main method begins by creating a variable called c and setting it to the cars.Audi value.

Next, the c.GetHashCode() method is called to get the integer value of the c  variable.. The GetHashCode() method returns a hash code for the current object, which can be used as an integer value to represent the object.

Finally, the Console.WriteLine() method is used to print out the original c variable and its corresponding integer value:

2: How to Get Int Value from Enum in C# Using Casting

Casting is a simple and straightforward way to get the integer value of an enum. Here is an example:

using System;
enum cars
{
    BMW,
  Mercedes,
   Audi,
   GMC
}
class Program
{
    static void Main(string[] args)
    {
        cars c = cars.Audi;
        int value = (int)c;
        Console.WriteLine("The integer value of {0} is {1}", c, value);
    }
}

 
First, the cars enumeration is defined in the same way as before, with four best car companies listed. Then, the c variable is created and set to the cars.Audi value.

Next, the int data type is used to cast the c variable to an integer value. This is done by wrapping the c variable with (int) parentheses.

Finally, the Console.WriteLine() method is used to print out the original c variable and its corresponding integer value.

3: How to Get Int Value from Enum in C# Using Convert.ChangeType()

The Convert.ChangeType() method is an efficient method in C# that can convert one type to another and in the case of enums we can use this method to convert an enum to an int, here is an example:

using System;

enum cars
{
    BMW,
  Mercedes,
   Audi,
   GMC
}
class Program
{
    static void Main(string[] args)
    {
        cars c = cars.Audi;
        int value = (int)Convert.ChangeType(c, typeof(int));
        Console.WriteLine("The integer value of {0} is {1}", c, value);
    }
}

 
This code defines an enumeration called cars that lists the four car companies and assigns the value cars.Audi to a variable called c. The Convert.ChangeType() method is then used to convert c to an integer value, which is stored in the value variable. Finally, the Console.WriteLine() method is used to print out the original c variable and its corresponding integer value:

4: How to Get Int Value from Enum in C# Using ToInt32

To get an int value from an enum in C#, you can use the built-in method called “Convert.ToInt32”. This method takes an object as a parameter and returns its equivalent integer value. To use this method, you need to cast your enum value to an object and here is an example:

using System;
enum cars
{
    BMW,
  Mercedes,
   Audi,
   GMC
}
class Program
{
    static void Main(string[] args)
    {
        cars c = cars.Audi;
       int value = Convert.ToInt32(c);
        Console.WriteLine("The integer value of {0} is {1}", c, value);
    }
}

 
First, the cars enumeration is defined in the same way as in the previous examples.Next, the c variable is created and set to the cars.Audi value.Then, the Convert.ToInt32() method is used to convert the c variable to its corresponding integer value. This method takes any object as an input and returns an integer value, so it can be used to convert an enum value to an integer.

Finally, the Console.WriteLine() method is used to print out the original c variable and its corresponding integer value.

Conclusion

Getting an int value from an enum in C# is a simple process. Overall, there are several ways to get an integer value from an enum in C#, including using Convert.ChangeType(), Object.GetHashCode(), and casting directly to an integer value. The Convert.ToInt32() method is another option that produces the same result.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.