Both of these answers are technically correct, but skip the explanation of the differences between constants and statics (read-only). In C #, constants are always faster than readonly, and the reason is very simple and best expressed by a small code example:
const int MyConstant = 10;
static readonly int MyReadonly = 20;
static void Main()
{
int result = MyConstant + MyReadonly;
}
. , . readonly, , , . :
static readonly Encoding = Encoding.GetEncoding("GB2132");
, , , GB2132. - . Static , readonly , . , .
, .
. . , :
enum MyEnum
{
First,
Second,
Third
}
static void Main()
{
MyEnum test = MyEnum.First;
if (test == MyEnum.Second)
{
}
}
:
const int MyEnum_First = 0;
const int MyEnum_Second = 1;
const int MyEnum_Third = 2;
static void Main()
{
int test = MyEnum_First;
if (test == MyEnum_Second )
{
}
}
, , , :
static void Main()
{
int test = 0;
if (test == 1 )
{
}
}