多种方式来定义的C#枚举用[国旗]属性? [英] Multiple ways to define C# Enums with [Flags] attribute?

查看:123
本文介绍了多种方式来定义的C#枚举用[国旗]属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知枚举在C#中是如何工作的,我得到什么标记属性带来的表。

我看到了这个问题,这里。其中建议第一的味道,但不为其提供任何原因/理由。

中是否存在,其中这两个被定义的方式的差异,是其中一个比另一个好?有哪些优势,使用第一synax作为,而不是第二个?在定义标记键入枚举......有我一直做错了这么长的时间?我一直使用的第二香味

  [Serializable接口]
[国旗]
公共枚举SiteRoles
{
    用户= 1&其中;&其中; 0,
    管理员= 1<< 1,
    服务台= 1<< 2
}
 

时那个不一样

  [Serializable接口]
[国旗]
公共枚举SiteRoles
{
    用户= 1,
    管理员= 2,
    服务台= 4
}
 

解决方案

与第一个主要的优点是你不需要计算正确值,每个标志,因为编译器会为你做它。除了它们是相同的。

I understand how Enums work in C#, and I get what the Flags attribute brings to the table.

I saw this question, here. Which recommends the first flavor, but doesn't provide any reason/justification for it.

Is there a difference in the way in which these two are defined, is one better than the other? What are the advantages to using the first synax as instead of the second? I've always used the second flavor when defining Flags type Enums... have I been doing it wrong all this time?

[Serializable]
[Flags]
public enum SiteRoles
{
    User = 1 << 0,
    Admin = 1 << 1,
    Helpdesk = 1 << 2
}

Is that not the same as

[Serializable]
[Flags]
public enum SiteRoles
{
    User = 1,
    Admin = 2,
    Helpdesk = 4
}

解决方案

The main advantage with the first one is that you don't need to calculate the correct values for each flag since the compiler will do it for you. Apart from that they are the same.

这篇关于多种方式来定义的C#枚举用[国旗]属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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