枚举和常数。何时使用? [英] Enums and Constants. Which to use when?
本文介绍了枚举和常数。何时使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
使用枚举,当你想定义一些值可以是。颜色是一个明显的例子,如:
public enum颜色
{
白色,
红色,
蓝色
}
或者也可能是一组可能的东西,如:
(示例我从 here 偷走,因为我很懒)
[FlagsAttribute]
枚举DistributedChannel
{
无= 0,
Transacted = 1,
Queued = 2,
加密= 4,
Persisted = 16,
FaultTolerant = Transact |排队|持久化
}
常量应为单个值,如PI。没有一个PI值范围,只有PI。
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
}
Or maybe a set of possible things like: (Example I stole from here as I'm lazy)
[FlagsAttribute]
enum DistributedChannel
{
None = 0,
Transacted = 1,
Queued = 2,
Encrypted = 4,
Persisted = 16,
FaultTolerant = Transacted | Queued | Persisted
}
Constants should be for a single value, like PI. There isn't a range of PI values, there is just PI.
这篇关于枚举和常数。何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文