English

Learn C# – How To Get Int Value from Enum?

Getting an int value from enum can be challenging in C#. However, it can easily be done by using the following code:

public enum Months {
       Numbers = 23,
       MonthType = 3
}

int abc = (int)Months.Number;

The above code stores int value of Months enum into abc.

We have used Typecasting to get the int value from Enum. Yes, it is as easy at it can get. But, didn’t we say it can be challenging? Let’s get to the challenging part.

If you don’t know what integral type is used (for example, int, byte, short, etc.), you can become stuck..

For example, enum is declared as below.

enum DifferentType {
     long WeInt = 1,
     short creat = 2.345
}

To get WeInt from the enum DifferentType, you need to use the following line of code:

long getWeInt = (long)DifferentType.WeInt

Either you need to know which integer type is used, or declare it as static class and know the constraints in it.

Let’s see an example of how it can be implemented.

public static class Learning
{
   public const int power= 2;
   public const int cleaning= 3;
   public const int hacker = 4;
   public const int creative = 5;
   public const int random = 6;
}

Now, you can just call Learning.power to get the value 2 without worrying about typecasting the right integer value.

However, you might want to use readonly as compiler tends to change them to hard values. You can read more about it here.

Another way of handling the underlying integer type.

If you don’t want to use const or readonly, then you can use GetTypeCode() method and convert a class to get your work done.

public enum Learning
{
  Power, hacker, creative, random
}
Learning Hacker = Learning.hacker
Object hack1 = Conver. ChangeType(hacker, Hacker.GetTypeCode());
Console.WriteLine(hack1);

And, that’s all for getting the int value from Enum. If you have any questions or feedback, use the comment section below.

You can also check on our website videos about C#. Below are some examples:

  • C# – Colossal Cave Adventure port – C#
  • Application Control of Acess in Real Time (part 2) – C#

You can also follow some of our broadcasters who program in C#, below:

 MauricioR

 David

Another cool way to find out interesting things about C# is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

  • The application control video is very informative. I also checked the project page it is wonderful.

Recent Posts

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

1 month ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

3 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

6 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

7 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

8 months ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

8 months ago