readonly VS const C#

readonly VS const

 

Key point:

-都表示常量

-Readonly 应用范围更广,const只能指定stringnull的引用类型和值类型。应为使用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;

            }

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值