C#7.3枚举约束:为什么不能使用enum关键字? [英] C# 7.3 Enum constraint: Why can't I use the enum keyword?

查看:512
本文介绍了C#7.3枚举约束:为什么不能使用enum关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了将泛型类型参数约束为枚举类型,我之前曾像这样约束它们,这是我在C#7.3之前的版本中为约束枚举类型T最好的方法:

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:

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

现在,C#7.3添加了新功能以将通用类型限制为System.Enum. 我尝试将枚举约束与今天发布的 VS2017 15.7更新,并且当我这样编写时(如果我有using System;指令),它将成功编译:

Now, C# 7.3 adds a new feature to constrain a generic type to 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):

void DoSomething<T>() where T : Enum

但是,使用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):

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

由于有struct约束适用于结构,所以我不明白为什么enum不适用于枚举.的确,enum不会映射到int的实际类型,但我认为它的行为应与struct约束相同.

Since there is a struct constraint working for structures, I do not understand why enum doesn't work here for enums. 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.

我只是陷入了一个尚未完全实现的实验性功能陷阱,还是在规范中故意这样做(为什么?)?

Did I just fall into an experimental feature trap not being fully implemented yet, or was this done on purpose in the specification (why?)?

推荐答案

对泛型的struct约束没有映射到实际的类型(尽管从理论上讲,它可以映射到

The struct constraint on generics doesn't map to an actual type (though it could, in theory, map to 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; hence public enum Stuff instead of 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.

因此,总的来说,是的,where T : enum并不起作用,因为enum是关键字,而不是类型别名.如果您真的希望看到它的工作,因为enum至少在此类上下文中闻起来像是类型别名,请提出要求!

So, in conclusion, yes, where T : enum is not meant to work because enum is a keyword, not a type alias. If you really want to see it work because enum at least smells like a type alias in context like these, go request it!

对于某些历史参考,这是2008年以来的一个问题表示Enum不是有效的约束,因为它是一个特殊的类.

For some historical reference, here's a question from 2008 indicating that Enum was not a valid constraint, since it's a special class.

这篇关于C#7.3枚举约束:为什么不能使用enum关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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