Home > Backend Development > C++ > How Can I Retrieve the Integer Value of a C# Enum Member?

How Can I Retrieve the Integer Value of a C# Enum Member?

Patricia Arquette
Release: 2025-01-28 13:01:39
Original
562 people have browsed it

How Can I Retrieve the Integer Value of a C# Enum Member?

In the c#, retrieve the integer value from the enumeration

Question:

This question deeply explores the integer values ​​of retrieval with enumeration. Consider the following scenes: a class named contains an enumeration called

, which means various survey issues. Is there a direct method to obtain an integer value of specific enumeration members and use it in functions such as

? Questions Question Answer: Questions.Get(Question.Role)

To extract the integer value from the enumeration, just convert it for mandatory. For example:

This forced conversion can perfectly cooperate with the vast majority of enumeration, because they usually use

as their underlying type.

<code class="language-csharp">int something = (int)Question.Role;</code>
Copy after login
However, it is important to note that enumeration may show different underlying types under certain circumstances. If enumeration is declared using ,

or int and other types, it must be forced to convert it into an enumerated underlying type. For example, consider the following enumeration:

uint In this case, the correct mandatory conversion will be: long ulong

By observing these compulsory conversion precautions, you can effectively retrieve the integer value associated with enumeration and use them in the code.
<code class="language-csharp">enum StarsInMilkyWay:long {Sun = 1, V645Centauri = 2, ..., Wolf424B = 2147483649};</code>
Copy after login

The above is the detailed content of How Can I Retrieve the Integer Value of a C# Enum Member?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template