Typesafe枚举模式 [英] Typesafe Enum Pattern

查看:81
本文介绍了Typesafe枚举模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要从我的枚举中获得一些额外的力量并实现

类型安全枚举模式。

它让我思考...为什么我应该使用标准枚举?


我现在写的一个很好的小代码片段给了我

即时实现模式。我可以很容易地总是

使用这样的实现而不是标准枚举,所以我想

知道你的专家都在想什么。


是否有标准枚举的情况?


我也注意到很难找到可以用模式实现的
的好例子在.NET上下文中。做谷歌(

typesafe enum pattern" -java)并且你得不到很多结果!


意见?

So I was needing some extra power from my enums and implemented the
typesafe enum pattern.
And it got me to thinking... why should I EVER use standard enums?

There''s now a nice little code snippet that I wrote today that gives me
an instant implementation of the pattern. I could easily just always
use such an implementation instead of a standard enum, so I wanted to
know what you experts all thought.

Is there a case for standard enums?

I also noticed its very hard to find good examples of what can be
achieved with the pattern in a .NET context. Do a google for (
"typesafe enum pattern" -java ) and you don''t get very many results!

Opinions?

推荐答案

C#枚举*是*类型安全的。没有非类型安全的

枚举。在C#中,枚举继承自值类型

System.Enum,这是一个结构。没有明确的演员阵容,就不会有枚举的混淆

成员和他们的完整对手。


至于你在谷歌搜索主题的困难,试着不要单词

" pattern" (即类型安全枚举)。当然,您找到的所有参考文献都将与Java编程语言相关,因为正如我所说,C#枚举

*是*类型安全的。


-

HTH,


Kevin Spencer

Microsoft MVP

Software Composer
http://unclechutney.blogspot.com

观看时钟永不沸腾。


Steven Nagy < le ********* @ hotmail.comwrote in message

news:11 ********************* *@i3g2000cwc.googlegro ups.com ...
C# enumerations *are* type-safe. There is no such thing as a non-type-safe
enumeration. In C#, enumerations are inherited from the value type
System.Enum, which is a structure. There can be no confusion of enumeration
members and their integral counterparts without an explicit cast.

As for your difficulty in Googling the subject, try leaving off the word
"pattern" (i.e. "typesafe enum"). Of course, all references you find will be
related to the Java programming language because, as I said, C# enumerations
*are* type-safe.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

A watched clock never boils.

"Steven Nagy" <le*********@hotmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...

所以我需要从我的枚举中获得额外的电量并实现

类型安全枚举模式。

它让我思考...为什么我应该使用标准枚举?


现在有一个很好的小代码片段我今天写的这个给了我

即时实现模式。我可以很容易地总是

使用这样的实现而不是标准枚举,所以我想

知道你的专家都在想什么。


是否有标准枚举的情况?


我也注意到很难找到可以用模式实现的
的好例子在.NET上下文中。做谷歌(

typesafe enum pattern" -java)并且你得不到很多结果!


意见?
So I was needing some extra power from my enums and implemented the
typesafe enum pattern.
And it got me to thinking... why should I EVER use standard enums?

There''s now a nice little code snippet that I wrote today that gives me
an instant implementation of the pattern. I could easily just always
use such an implementation instead of a standard enum, so I wanted to
know what you experts all thought.

Is there a case for standard enums?

I also noticed its very hard to find good examples of what can be
achieved with the pattern in a .NET context. Do a google for (
"typesafe enum pattern" -java ) and you don''t get very many results!

Opinions?



我以某种方式发现在C#

规范中写出了相当多的枚举,这非常好:
http://msdn.microsoft.com/netframewo ... r / default.aspx


chanmm


" Steven Nagy" < le ********* @ hotmail.comwrote in message

news:11 ********************* *@i3g2000cwc.googlegro ups.com ...
I somehow found that there is quite an amount of enum write out in C#
specification which is quite good:
http://msdn.microsoft.com/netframewo...r/default.aspx

chanmm

"Steven Nagy" <le*********@hotmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...

所以我需要从我的枚举中获得额外的电量并实现

类型安全枚举模式。

它让我思考...为什么我应该使用标准枚举?


现在有一个很好的小代码片段我今天写的这个给了我

即时实现模式。我可以很容易地总是

使用这样的实现而不是标准枚举,所以我想

知道你的专家都在想什么。


是否有标准枚举的情况?


我也注意到很难找到可以用模式实现的
的好例子在.NET上下文中。做谷歌(

typesafe enum pattern" -java)并且你得不到很多结果!


意见?
So I was needing some extra power from my enums and implemented the
typesafe enum pattern.
And it got me to thinking... why should I EVER use standard enums?

There''s now a nice little code snippet that I wrote today that gives me
an instant implementation of the pattern. I could easily just always
use such an implementation instead of a standard enum, so I wanted to
know what you experts all thought.

Is there a case for standard enums?

I also noticed its very hard to find good examples of what can be
achieved with the pattern in a .NET context. Do a google for (
"typesafe enum pattern" -java ) and you don''t get very many results!

Opinions?



谢谢Kevin。


是的我知道枚举是类型安全的。我对实际的

模式更感兴趣。

这不是我的问题所涉及的类型安全的方面,并且

我假设在这种情况下,我的问题可能会产生误导。


该模式基于提供一个类而不是一个枚举和

,从而实现更多的潜力课程比enum可以。我的

利息就是这个,人们发现这个模式对于

有用,或者也许是人们发现的标准

enum规定模式的实现不能。


谢谢,

史蒂文


Kevin Spencer写道:
Thanks Kevin.

Yes I know enums are type-safe. I was more interested in the actual
pattern.
Its not the aspect of being type-safe that my question relates to, and
I suppose in that context my question could have been misleading.

The pattern is based around providing a class instead of an enum and
thus realising more potential as a class than an enum could. My
interest is around that, and what people have found to be useful about
the pattern, or perhaps what people have discovered that a standard
enum provides that an implementation of the pattern could not.

Thanks,
Steven

Kevin Spencer wrote:

C#枚举*是*类型安全的。没有非类型安全的

枚举。在C#中,枚举继承自值类型

System.Enum,这是一个结构。没有明确的演员阵容,就不会有枚举的混淆

成员和他们的完整对手。


至于你在谷歌搜索主题的困难,试着不要单词

" pattern" (即类型安全枚举)。当然,您找到的所有引用都将与Java编程语言相关,因为正如我所说,C#枚举

*是*类型安全的。
C# enumerations *are* type-safe. There is no such thing as a non-type-safe
enumeration. In C#, enumerations are inherited from the value type
System.Enum, which is a structure. There can be no confusion of enumeration
members and their integral counterparts without an explicit cast.

As for your difficulty in Googling the subject, try leaving off the word
"pattern" (i.e. "typesafe enum"). Of course, all references you find will be
related to the Java programming language because, as I said, C# enumerations
*are* type-safe.


这篇关于Typesafe枚举模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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