枚举和常量。它何时使用? [英] Enums and Constants. Which to use when?
本文介绍了枚举和常量。它何时使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做的一些枚举阅读和发现它们非常相似,声明常量。我怎么知道什么时候使用恒定的,而不是一个枚举,反之亦然。什么是一些使用枚举的优势是什么?
I was doing some reading on enums and find them very similar to declaring constants. How would I know when to use a constant rather than an enum or vice versa. What are some of the advantages of using enums?
推荐答案
当你要定义一个范围值的东西可以使用枚举。色彩是一个明显的例子,如:
Use enums when you want to define a range of values that something can be. Colour is an obvious example like:
public enum Colour
{
White,
Red,
Blue
}
也许一组类似的事情可能:
(例如我从<一个偷href=\"http://mark.michaelis.net/Blog/FlagsAttributeAndBitwiseORWithoutTheFlagsAttribute.aspx\">here因为我懒)
[FlagsAttribute]
enum DistributedChannel
{
None = 0,
Transacted = 1,
Queued = 2,
Encrypted = 4,
Persisted = 16,
FaultTolerant = Transacted | Queued | Persisted
}
常数应是一个单一的值,如PI。没有一个范围PI值中,只有PI。
Constants should be for a single value, like PI. There isn't a range of PI values, there is just PI.
这篇关于枚举和常量。它何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文