【发布时间】:2022-09-24 16:43:44
【问题描述】:
我来自 C/C++,使用过很多类似 #define OBJ_STATE_INPROCESS 2 的东西,所以在编写实际逻辑时你可以使用 state = OBJ_STATE_INPROCESS;,它的作用比 state = 2; 更明显,这使得代码更易于维护。
我想知道C#中是否有这样的技巧
-
您是否在寻找: const int ObjStateInProgress = 2; ?
标签: c#
我来自 C/C++,使用过很多类似 #define OBJ_STATE_INPROCESS 2 的东西,所以在编写实际逻辑时你可以使用 state = OBJ_STATE_INPROCESS;,它的作用比 state = 2; 更明显,这使得代码更易于维护。
我想知道C#中是否有这样的技巧
标签: c#
尽管在技术上是一个不同的概念,但在 C# 中,您可以使用常量和枚举来避免“幻数”,例如
public static class Constants
{
public const string MyConst = "ThisIsMyConst";
}
public enum MyEnum
{
MyEnumValue1,
MyEnumValue2,
}
// Usage
var value = MyEnum.MyEnumValue2;
【讨论】: