C # const and readonly keywords

Foreword

  I do not know if you have any understanding of the difference between const and readonly two keywords, which in the end really do not know how it was before I realize I, so if you are not very clear, you can work together to explore. In these two keywords understand when we start to look at static and dynamic constants constants.

  Static constant: means the compiler will be resolved at compile time constants, replace that value to initialize the value and constant.

  The dynamic constant value is at the moment running only obtained during the compiler will be marked as read-only constants, but do not replace the value of the constant, so that the dynamic constants do not have to initialize when it was declared, and may be delayed until constructor initializes. Let us now explain const and readonly it.

readonly and const

  const constant is modified in the above-described first, i.e. constant static; the second is readonly, i.e. the dynamic constants. Then the difference can be explained by the characteristics of static and dynamic constants constants:

   1, const modified constants must be initialized when declared; readonly constants can be modified to delay the constructor to initialize 

        2, const modified constant during compilation is parsed, i.e., a constant value is replaced with initialized value; Readonly modified to delay the constant running time

   3, const modified constant focus is on efficiency; readonly modified constant focus on flexibility

   4, const modified constant no memory consumption; readonly constant because of the need to save, so there is memory consumption

   5, const only modifies primitive types, enumeration class or type string; Readonly do not have this restriction

 

The first example:

If you add up in front of a static const, then it is obviously wrong, because then the compiler is already static const static field of

After compiling const variable will be replaced with the variable's value, which is why it is a high efficiency.

1 const double pi = 3.1415926;
2 static void Main(string[] args)
3 {
4       Console.WriteLine(pi);
5       Console.ReadLine();
6 }

After the code is compiled print variable is already

Console.WriteLine((double) 3.1415926);

 

A second example:

Let's look at a small example readonly

 1     class Person
 2     {
 3         public readonly string Name;
 4         public Person(string name)
 5         {
 6             this.Name = name;
 7         }
 8     }
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Person person = new Person("aehyok");
14             person.Name = "Leo";
15             Console.ReadLine();
16         }
17     }

Person instance variable name is not assigned in the constructor, following code after compilation error

Next we look at another case

 1     class Person
 2     {
 3         public string Name;
 4         public Person(string name)
 5         {
 6             this.Name = name;
 7         }
 8     }
 9 
10     class Worker
11     {
12         public readonly Person Person;
13         public Worker(Person person)
14         {
15             this.Person = person;
16         }
17     }
18     class Program
19     {
20         static void Main(string[] args)
21         {
22             Worker worker = new Worker(new Person("aehyok"));
23             worker.Person = new Person("Leo");
24 
25             worker.Person.Name = "Leo";
26 
27             Console.ReadLine();
28         }
29     }

来看编译后的效果

这一点不知是否可以说是readonly的灵活呢

 

第三个例子:

 1     class Program
 2     {
 3         static readonly int A = B * 10;
 4         static readonly int B = 10;
 5         public static void Main(string[] args)
 6         {
 7             Console.WriteLine("A is {0},B is {1} ", A, B);
 8             Console.ReadLine();
 9         }
10     }

结果很简单

道理也比较简单,就是static readonly是动态常量,变量的值在编译期间不予以解析,所以开始都是默认值,像A与B都是int类型,故都是0。而在程序执行到A=B*10;所以A=0*10=0,程序接着执行到B=10这句时候,才会真正的B的初值10赋给B。

总结

   对于const和readonly这两个关键字目前来说,也算是有所了解了,还是学了不少东西,如果看完本文还有疑问的话,你可以通过微软提供的ILDASM工具。

打开cmd命令之后,输入ILDASM。

打开exe文件进行查看IL执行过程,慢慢研究吧。 

Guess you like

Origin www.cnblogs.com/ybqjymy/p/12486958.html