Reputation: 77
I came from C/C++, and used alot things like #define OBJ_STATE_INPROCESS 2
, so that when coding actual logic you can have state = OBJ_STATE_INPROCESS;
, and what it does become more obvious than state = 2;
, which makes the code easier to maintain.
I wonder if there is some trick like this in C#
Upvotes: 1
Views: 56
Reputation: 2485
You can accomplish that using constant properties, for example:
static class Constants
{
public const int OBJ_STATE_INPROCESS = 2
}
class Program
{
static void Main()
{
Console.WriteLine(Constants.OBJ_STATE_INPROCESS ); //prints 2
}
}
Upvotes: 1
Reputation: 22481
Though technically a different concept, in C# you can use contants and enums to avoid "magic numbers", e.g.
public static class Constants
{
public const string MyConst = "ThisIsMyConst";
}
public enum MyEnum
{
MyEnumValue1,
MyEnumValue2,
}
// Usage
var value = MyEnum.MyEnumValue2;
Upvotes: 3