Comparaison between const and readonly in C#

/* By Dylan SUN */

Here let’s talk about const and readonly.

const is considered as compile-time constant readonly is considered as runtime constant.

I’ll demonstrate two example code to clarify their usages and differences.

Const usages

Firstly, you can see that const variable can be declared and assigned at class level.

public class ConstExample{CstValue1 = “hello”; //Correct}

You can also declare a const variable in constructor

public ConstExample(){const string cstValue6 = “hehe”; //Correct}

You can see that const can be declared at method level too

(){const string cstValue2 = “cst 2”; //Correct : Constant can be declared in method}

If you want to assign another value to CstValue1, you will get an compile-time exception. Because const can only be assigned when declaration.

resolve symbol CstValue1

You can not also assign another value to CstValue1 in the constructor. You will get an compile-time exception.

public ConstExample(){CstValue1 = “hello2”; //Compile time exception : Constant can not be used as an assignment target}

May be you want to assign a normal string variable to a const like this. But you will see it doesn’t work too.

public string v1;const string CstValue2 = v1; //Compile time exception : Constant initializer must be compile-time constant

But you can assign the operation result of several consts to another const like this.

CstValue3 = CstValue4 = CstValue1 + CstValue3; //Correct : Constants can only be assigned with values or with consts

You can also declare a const, and assign it with the addition of two consts in the method.

const string cstValue5 = CstValue1 + CstValue3;

When you display them you can see

Console.WriteLine(CstValue1); //=> helloConsole.WriteLine(cstValue2); //=> cst 2Console.WriteLine(CstValue4); //=> helloworldConsole.WriteLine(cstValue5); //=> helloworldreadonly

Firstly, you can declare an readonly variable at class level.

public class ReadOnlyExample{rdValue1 = “good”;}

You cannot assign a value to variable rdValue1 after the declaration, except for, you assign a value in the class constructor.

rdValue1 = resolve symbol rdValue1

You can also just declare a readonly variable without assigning any value to it.

rdValue2;

You can not assign a readonly variable to another readonly variable at class level.

readonly string rdValue3 = rdValue1; //Compile time exception : cannot access non-static field ‘rdValue1’ in static context

You can not assign a normal variable to a readonly variable neither.

public string value1 = “hey”;readonly string rdValue6 = value1; //Compile time exception : cannot access non-static field ‘value1’ in static context

But you can assign a normal variable to a readonly variable in class constructor. You can event assign the operation result of several readonly variables to another readonly variable.

rdValue4;rdValue5;){rdValue2 = value; //CorrectrdValue4 = rdValue1 + rdValue2; //Correct : Assign another readonly variable to another readonly variable can only be done in constructorrdValue5 = rdValue1 + value1; //Correct : Assign readonly variable with normal variable to another readonly variable}

You can not assign value to a readonly variable in a method.

(){rdValue1 = “good one”; //Compile time exception: Readonly field can not be used as an assignment targetrdValue2 = “good too”; //Compile time exception: Readonly field can not be used as an assignment target}

You can not declare a readonly variable in a method.

readonly string rdValue6 = “hohoho”; //compile time exception : statement expected

when you display them, you will see

Console.WriteLine(rdValue1); //=> goodConsole.WriteLine(rdValue2); //=> dayConsole.WriteLine(rdValue4); //=> gooddayConsole.WriteLine(rdValue5); //=> goodhey

If you want to access to a const, you must access it via the class

Console.WriteLine(ConstExample.CstValue1); //Const variable can only be accessed by class

If you want to access to a readonly variable, you must access it via an instance of class.

var re = new ReadOnlyExample(“day”);Console.WriteLine(re.rdValue1); //Readonly value can only be accessed by instance of class

So to conclude,

Const :

Can only be assigned at declaration

Can be declared at class level, in constructor and in method.

Can be assigned with operation result of several consts at class level or in constructor or in method. (like addition, multiplication etc)

Once the declaration is done, you can never modify a const’s value, neither in constructor, nor just after declaration.

You can access const variable only by class.

Readonly:

Can only be assigned at declaration or in constructor.

Can be declared only at class level. You can declare a readonly variable neither in constructor nor in method.

我们摇摇头说,困难其实没什么大不了。

Comparaison between const and readonly in C#

相关文章:

你感兴趣的文章:

标签云: