Tag: 枚举

即使指定了基础类型,也无法从枚举中隐式转换值

在下面的代码示例中,我定义了一个枚举,并将其基础类型指定为byte。 然后我尝试分配一个字节值并打开枚举值但我得到一个错误: Cannot implicitly convert type ‘CmdlnFlags’ to ‘byte’. An explicit conversion exists (are you missing a cast?) Cannot implicitly convert type ‘CmdlnFlags’ to ‘byte’. An explicit conversion exists (are you missing a cast?) 代码: using System; public enum CmdlnFlags: byte { ValA = (byte)’a’, ValB = (byte)’b’, } public class Sample { public static […]

使用属性定义C#枚举的多种方法?

我理解Enums如何在C#中工作,我得到了Flags属性带给表的内容。 我在这里看到了这个问题。 其中推荐了第一种口味,但没有提供任何理由/理由。 两者的定义方式是否存在差异,哪一个比另一个好? 使用第一个synax代替第二个synax有什么好处? 在定义Flags类型Enums时,我总是使用第二种风格……我一直都在做错吗? [Serializable] [Flags] public enum SiteRoles { User = 1 << 0, Admin = 1 << 1, Helpdesk = 1 << 2 } 那是不一样的 [Serializable] [Flags] public enum SiteRoles { User = 1, Admin = 2, Helpdesk = 4 }

如何将枚举数作为常量?

根据枚举中定义的项目总数 ,我看到我可以使用以下命令获取枚举数: Enum.GetNames(typeof(Item.Type)).Length; 效果很好! 但是,我需要将此数字作为常数,以便我可以在Unity的[Range(int, int)]function中使用它。 private const int constEnumCount = Enum.GetNames(typeof(Item.Type)).Length; 以上不起作用,因为枚举计数不是常数,所以我不能将它分配给常量变量。 如何将枚举数作为常量?

将枚举转换为整数失败时,为什么不能获得InvalidCastException?

public enum Animal { Dog = 1, Cat = 2, Cow = 3 } int animalID = 4; if ((Animal)animalID == Animal.Dog) // does not throw exception animalID无法投放到Animal 。 将枚举InvalidCastException为整数失败时,为什么不能获得InvalidCastException ?

C#枚举 – 针对面具检查标志

我有以下枚举标志: [Flags] private enum MemoryProtection: uint { None = 0x000, NoAccess = 0x001, ReadOnly = 0x002, ReadWrite = 0x004, WriteCopy = 0x008, Execute = 0x010, ExecuteRead = 0x020, ExecuteReadWrite = 0x040, ExecuteWriteCopy = 0x080, Guard = 0x100, NoCache = 0x200, WriteCombine = 0x400, Readable = (ReadOnly | ReadWrite | ExecuteRead | ExecuteReadWrite), Writable = (ReadWrite […]

const int而不是enum的列表

我开始研究一个大的c#代码库,发现使用了一个带有几个const int字段的静态类。 这个类的行为与枚举完全相同。 我想把这个类转换成一个实际的枚举,但是权力可以说是没有。 我想转换它的主要原因是我可以将枚举作为数据类型而不是int。 这对可读性有很大帮助。 是否有任何理由不使用枚举并使用const int代替? 目前这是代码的方式: public int FieldA { get; set; } public int FieldB { get; set; } public static class Ids { public const int ItemA = 1; public const int ItemB = 2; public const int ItemC = 3; public const int ItemD = 4; public const int […]

是否有可能保证枚举的ToString的值是多少?

我正在使用的数据库目前有一个varchar字段,在我的代码中我想将潜在的值映射到枚举,如: public enum UserStatus { Anonymous, Enrolled, SuperUser } 在此列的数据库级别,对其进行约束,其值必须为: ANONYMOUS ENROLLED SUPERUSER 我有可能这样做: UserStatus.SuperUser.ToString() 并且这个价值是超级的,这是一致的,而不是搞砸了吗?

键值对作为枚举

我可以使用枚举作为键值对。 public enum infringementCategory { Infringement, OFN } 如果我选择Infringement我应该得到“INF0001”,如果我选择OFN我应该得到“INF0002” 可能吗?

“类,其中T:Enum”不起作用

可能重复: 创建将T限制为枚举的通用方法 我们有什么理由不能在C#中做到这一点吗? 而且,如果可能的话,我该怎么做类似的事情! 我想要的是 : public class ATag where T : enum { [Some code ..] } public class classBase where T : enum { public IDictionary tags { get; set; } } 所以,当它到了调用它的时候,我只能得到一个我的枚举值。 public class AClassUsingTag : classBase { public void AMethod(){ this.tags.Add(PossibleTags.Tag1, “Hello World!”); this.tags.Add(PossibleTags.Tag2, “Hello Android!”); } } public enum PossibleTags […]

C#:Enum.IsDefined在组合标志上

我有这个枚举: [Flags] public enum ExportFormat { None = 0, Csv = 1, Tsv = 2, Excel = 4, All = Excel | Csv | Tsv } 我试图在这个(或任何,真的)枚举上做一个包装,它通知变化。 目前它看起来像这样: public class NotifyingEnum : INotifyPropertyChanged where T : struct { private T value; public event PropertyChangedEventHandler PropertyChanged; public NotifyingEnum() { if (!typeof (T).IsEnum) throw new ArgumentException(“Type T […]