简体   繁体   English

C# 7.3 Enum 约束:为什么我不能使用 enum 关键字?

[英]C# 7.3 Enum constraint: Why can't I use the enum keyword?

To constrain a generic type parameter to be of an enum type, I previously constrained them like this, which was the best I could go for constraining type T for enums in pre-C# 7.3:为了将泛型类型参数约束为枚举类型,我之前像这样约束了它们,这是在 C# 7.3 之前的枚举中约束类型 T 的最佳方法:

void DoSomething<T>() where T : struct, IComparable, IConvertible, IFormattable

Now, C# 7.3 adds a new feature to constrain a generic type to System.Enum .现在,C# 7.3 添加了一个新功能来将泛型类型限制为System.Enum I tried using the enum constraint with the VS2017 15.7 update released today , and it compiles successfully when I write it like this (given I have a using System; directive):我尝试在今天发布VS2017 15.7 更新中使用 enum 约束,当我这样写时它编译成功(假设我有一个using System;指令):

void DoSomething<T>() where T : Enum

However, using the enum keyword does not work and causes the compiler to throw the following errors (there are more errors following, expecting a method body, but not really worth mentioning here I guess):但是,使用enum关键字不起作用并导致编译器抛出以下错误(后面还有更多错误,需要一个方法体,但我猜这里并不值得一提):

void DoSomething<T>() where T : enum
                                ^ error CS1031: Type expected
                                  error CS1002: ; expected
                                    ^ error CS1001: Identifier expected
                                      error CS1514: { expected
                                      error CS1513: } expected

Since there is a struct constraint working for structures, I do not understand why enum doesn't work here for enums.由于有一个适用于结构的struct约束,我不明白为什么enum在这里不适用于枚举。 It's true that enum does not map to an actual type like int would do for Int32 , but I thought it should behave the same as the struct constraint.确实enum不会像int那样映射到Int32那样的实际类型,但我认为它的行为应该与struct约束相同。

Did I just fall into an experimental feature trap not being fully implemented yet, or was this done on purpose in the specification (why?)?我是否只是陷入了尚未完全实现的实验性功能陷阱,还是在规范中故意这样做(为什么?)?

The struct constraint on generics doesn't map to an actual type (though it could, in theory, map to ValueType ).泛型上的struct约束不映射到实际类型(尽管理论上它可以映射到ValueType )。 Similarly, enum doesn't cleanly map to actual types the way string , int , or long do, it sets up special syntax for creating a class of symbolic constants that map to integer values;类似地, enum并没有像stringintlong那样干净地映射到实际类型,它设置了特殊的语法来创建一类映射到整数值的符号常量; hence public enum Stuff instead of public class Stuff : Enum .因此public enum Stuff而不是public class Stuff : Enum Note that had the latter been implemented instead, it would be more subtle since it would change syntax based on inherited type, instead of changing syntax based on a non- class keyword.请注意,如果改为实现后者,它会更加微妙,因为它会根据继承的类型更改语法,而不是根据非class关键字更改语法。

So, in conclusion, yes, where T : enum is not meant to work because enum is a keyword, not a type alias.所以,总而言之,是的, where T : enum并不意味着工作,因为enum是一个关键字,而不是一个类型别名。 If you really want to see it work because enum at least smells like a type alias in context like these, go request it!如果你真的想看到它工作,因为enum至少在这样的上下文中闻起来像一个类型别名,去请求它!

EDIT: For some historical reference, here's a question from 2008 indicating that Enum was not a valid constraint, since it's a special class.编辑:对于一些历史参考, 这里是 2008 年的一个问题,表明Enum不是有效的约束,因为它是一个特殊的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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