简体   繁体   中英

Why does int.Parse not work on an Enum value?

can you tell me how this first line converts to int and second doesn't work?

public enum SomeEnumerator { AndHisValue, second, third }

int a = (int)(SomeEnumerator.AndHisValue);

int a = int.Parse(SomeEnumerator.AndHisValue);

Type of AndHisValue is string.

Image

As you show in your image, SomeEnumerator is not of type string, but instead an enum .

string constants are denoted by " around them. enum constants, such as SomeEnumerator.AndHisValue are their own kind of value, each usually having their own distinct value.

You are allowed to directly cast any enum -value to int or string , but as int.Parse() expects a string you cannot use your enumerator value here

(see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum as reference for enums)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM