IAm*_*00B 3 c# performance enums static-classes
这两个项目之间的性能影响是什么?我最近在野外见过静态类,我不知道该怎么做.
public enum SomeEnum
{
One = 1,
Two,
Three
}
public static class SomeClass
{
public static readonly int One = 1;
public static readonly int Two = 2;
public static readonly int Three = 3;
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 15
不同的是类型安全.假设你有两个这样的枚举.你怎么说出差异:
void SomeMethod(int x, int y)
// Compiles, but won't do what you want.
SomeMethod(SomeOtherClass.Xyz, SomeClass.One);
Run Code Online (Sandbox Code Playgroud)
VS
void SomeMethod(SomeEnum x, SomeOtherEnum y)
// Compile-time error
SomeMethod(SomeOtherEnum.Xyz, SomeEnum.One)
Run Code Online (Sandbox Code Playgroud)
因此,无论您在哪里都有一个想要成为特定值集的表达式,如果您使用枚举,您可以向读者和编译器明确表示您感兴趣的值集.只有整体......不是那么多.
| 归档时间: |
|
| 查看次数: |
4114 次 |
| 最近记录: |