readonly VS const
Key point:
-都表示常量
-Readonly 应用范围更广,const只能指定string和null的引用类型和值类型。应为使用const的要求是can be fully evaluated atcompile time
-Const效率略高,因为static by default
何时使用(When to use)?
Readonly:runtime, once given value(initialized), it will not be able to be changed.
Const:compile time, 程序编译时将对常量值进行解析,并将所有常量引用替换为相应值。By default it is static
(声明和初始化)Declaration and initialize:
Readonly:
The readonly keywordis a modifier that you can use on fields. When a field declaration includesa readonly modifier, assignments to the fields introduced by thedeclaration can only occur as part of thedeclaration or in a constructor in the same class.
classAge
{
readonlyint _year;
Age(intyear)
{
_year = year;
}
voidChangeYear()
{
//_year= 1967; // Compile error if uncommented.
}
}
the readonly fieldcan be used for runtime constants as in the following example:
public staticreadonly uint timeStamp = (uint)DateTime.Now.Ticks;
Const:
A const field can only be initialized at thedeclaration of the field.
The const keyword is used to modify a declaration of afield or local variable. It specifies that the value of the field or the localvariable is constant, which means it cannot be modified. For example:
const int x =0;
public const double gravitationalConstant = 6.673e-11;
private const string productName = "Visual C#";
The type of a constantdeclaration specifies the type of the members introduced by the declaration. Aconstant expression must yield a value of the target type, or of a type that can be implicitly converted to the targettype.
A constant expression isan expression that can be fully evaluated at compile time.Therefore, the only possible values for constants of referencetypes are string and null.
The constant declarationcan declare multiple constants, such as:
publicconst double x = 1.0, y = 2.0, z = 3.0;
The static modifier is not allowed in a constant declaration.
A constant can participatein a constant expression, as follows:
publicconst int c1 = 5;
public const int c2 = c1 + 100;
Other:
Const will not changewithout recompile.
Readonly can alwaysget the right value.
Example:
Which one is correct?
public class TimerState
{
private String m_state;
public static readonly TimerStateInOffset = new TimerState("InOffset");
public static readonly TimerStateIdle = new TimerState("Idle");
public static readonly TimerStateInDuration = new TimerState("InDuration");
public static readonly TimerStateWaitingForCycleStart = new TimerState("WaitingForCycleStart");
private TimerState(String state)
{
m_state = state;
}
public override string ToString()
{
return m_state;
}
}
public class TimerState1
{
private String m_state;
public const TimerState1 InOffset =new TimerState1("InOffset");
public const TimerState1 Idle = newTimerState1("Idle");
public const TimerState1 InDuration= new TimerState1("InDuration");
public const TimerState1WaitingForCycleStart = new TimerState1("WaitingForCycleStart");
private TimerState1(String state)
{
m_state = state;
}
public override string ToString()
{
return m_state;
}
}