C# 中的 Java 枚举相当于什么?
Java 中的 enum 相当于 C# 中的什么?
What's the equivalent of Java's enum in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Java 中的 enum 相当于 C# 中的什么?
What's the equivalent of Java's enum in C#?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
C# 中不提供完整的 Java 枚举功能。不过,您可以使用嵌套类型和私有构造函数来合理接近。例如:
当然,您不必使用嵌套类型,但它们提供了 Java 枚举所擅长的方便的“自定义行为”部分。在其他情况下,您只需将参数传递给私有构造函数即可获取众所周知的受限值集。
这不会给你一些东西:
EnumSet
其中一些可能可以通过足够的努力来完成,尽管如果没有的话 switch 就不太可行黑客行为。现在,如果语言做了这样的事情,它可以通过使黑客自动化(例如自动声明
const
字段的负载,并更改任何将枚举类型切换为整数切换,仅允许“已知”情况。)哦,部分类型意味着您不必在同一个文件中拥有所有枚举值。如果每个值都非常复杂(这绝对是可能的),每个值都可以有自己的文件。
Full Java enum functionality isn't available in C#. You can come reasonably close using nested types and a private constructor though. For example:
Of course you don't have to use nested types, but they give the handy "custom behaviour" part which Java enums are nice for. In other cases you can just pass arguments to a private constructor to get a well-known restricted set of values.
A few things this doesn't give you:
EnumSet
Some of that could probably be done with enough effort, though switch wouldn't really be feasible without hackery. Now if the language did something like this, it could do interesting things to make switch work by making the hackery automatic (e.g. declaring a load of
const
fields automatically, and changing any switch over the enum type to a switch over integers, only allowing "known" cases .)Oh, and partial types mean you don't have to have all of the enum values in the same file. If each value got quite involved (which is definitely possible) each could have its own file.
枚举是在 java 中比 c# 实现得更好的少数语言功能之一。
在 java 中,枚举是类型的完整命名实例,而 c# 枚举基本上是命名常量。
话虽如此,对于基本情况,它们看起来很相似。然而,在 java 中,您拥有更多的功能,因为您可以向各个枚举添加行为,因为它们是成熟的类。
您是否特别需要某些功能?
Enums are one of the few language features that is better implemented in java than c#.
In java, enums are full fledged named instances of a type, while c# enums are basically named constants.
That being said, for the basic case, they will look similar. However in java, you have more power, in that you can add behavior to the individual enums, as they are full fledged classes.
is there some feature in particular you are looking for?
这是另一个有趣的想法。我想出了以下
Enumeration
基类:它有一个类型参数,基本上这样序数计数就可以在不同的派生枚举中正常工作。上面 Jon 的
Operator
示例变为:这提供了一些优点。
string
和int
,这使得 switch 语句可行Here's another interesting idea. I came up with the following
Enumeration
base class:It's got a type parameter basically just so the ordinal count will work properly across different derived enumerations. Jon's
Operator
example above then becomes:This gives a few advantages.
string
andint
which makes switch statements feasibleSystem.Enum
can be added to the base Enumeration class to allow the same functionality.您可能可以使用我们在获得真正的枚举模式之前在 Java 中使用的旧类型安全枚举模式(假设 C# 中的枚举模式确实不是注释所声明的类)。该模式在此页面的中间部分进行了描述
You could probably use the old typesafe enum pattern that we used in Java before we got real ones (assuming that the ones in C# really aren't classes as a comment claims). The pattern is described just before the middle of this page
enum ,或者您需要以下内容特别是 Java 枚举有但 C# 没有?
enum , or do you need something in particular that Java enums have but c# doesn't ?