枚举为int。 INT到枚举。做转换的最佳方式? [英] Enum to Int. Int to Enum . Best way to do conversion?

查看:146
本文介绍了枚举为int。 INT到枚举。做转换的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 public Enum Days
 {
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6,
    Sunday = 7
 }

现在我想知道我怎么得到一个枚举的整数值,并转换成一个整数值到枚举

Now I wanted to know how do I get the integer value of an enum and convert an integer value into enum

威尔

       int dayNo = (int) Days.Monday;

dayNo的值更改为1;

change the value of dayNo to 1;

威尔

        Days day = (Days) 2;

分配Days.Tuesday给变量的一天?

assign Days.Tuesday to the variable day ??

怎么样,这是做解析??最好的办法

How about is this the best way to do the parsing??

推荐答案

是的,它会做完全,除了枚举枚举

Yes it will do exactly that except Enum should be enum

public enum Days
 {
    Monday = 1,
    Tuesday = 2,
    Wednesday = 3,
    Thursday = 4,
    Friday = 5,
    Saturday = 6,
    Sunday = 7
 }

要使用,所以如果你想投从INT你必须通过一个字符串,它是丑陋去Enum.Parse必须提供的字符串。

To use Enum.Parse you MUST provide a string so if you want to cast from the int you'd have to go via a string which is ugly.

Days x = (Days)Enum.Parse(typeof(Days), "3");
Days y = (Days)Enum.Parse(typeof(Days), 3.ToString());

...既给你星期三。

... both give you wednesday.

这篇关于枚举为int。 INT到枚举。做转换的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆