Constants and fields are both data members of the type, but the difference is great.
1. The constant value will never change. Fields are of various types, and the values of non-read-only fields can be changed.
2. The constant value must be determined during compilation, that is, it must be assigned a value during definition. After compilation, the constant value is saved in the Assembly metadata. The field is stored in the dynamic memory and can be obtained at runtime.
3. the definition of a constant must use the primitive type. For details about the primitive type, see (CLR Via C # learning notes (1) primitive type value type reference type). The field definition can be of any type.
4. because the value of a constant cannot be changed, we can regard it as a static type. In the IL code, we can see that the constant has a static modifier, so it is the same as calling a static field during the call, directly use the class name. constant name; the static field in the field can be called directly using the class name just like the constant. field name. the instance of the class is required to call non-static fields. ,
5. C # static modification of constants is not allowed, because constants themselves are implicitly of the static type. static can be used for fields, and static is used to define fields as static fields.
Let's take an example to understand constants.
1. Create a class library project named Oec2003ClassLibrary. Write the following code in the default class Class1:
namespace Oec2003ClassLibrary{ public class Class1 { public const double PI = 3.14; public static double _pi = 3.14; }}
2. Create a web project, add a reference to the Oec2003ClassLibrary class library project, create the ConstTest. aspx page, and write the following code in PageLoad:
protected void Page_Load(object sender, EventArgs e){ Response.Write(Class1.PI+"<br/>"); Response.Write(Class1._pi);}
3. Set ConstTest as the start page and run it. The result is as follows:
4. Rewrite the Class1 code in the Oec2003ClassLibrary project as follows and re-compile the project.
namespace Oec2003ClassLibrary{ public class Class1 { public const double PI = 3.1415926; public static double _pi = 3.1415926; }}
5. Refresh the page. The result is as follows:
6. Run the ConstTest page and you will see the following results:
From the above example, we can see that the constant value will never change when the application is not re-compiled. If an application wants to obtain a new value of a constant, it must be re-compiled. Therefore, a constant cannot be used if one application Assembly wants to obtain the value of another application assembly at runtime, read-Only fields can be used ).
The field is also a type of data member. The modifier of the field is static readonly volatile. The field without the above modifier is a common instance field. Static can be used with readonly, which is a static read-only field. Volatile will be described in later sections.
Notes about readonly
L 1 when the field modified by readonly is of the value type, if the value of the field is changed in the view during the call, the compilation will fail.
L 2 when the field modified by readonly is of the reference type, the reference of the field cannot be changed, but the value of the Application object can be changed. See the example below.
1. Write the following code in the Class1.cs file of the Oec2003ClassLibrary project:
namespace Oec2003ClassLibrary{ public class Class1 { public const double PI = 3.14; public static readonly double _pi = 3.14; public static readonly User user = new User("oec2003"); } public class User { public User(string name) { Name = name; } public string Name { get; set; } }}
2. Add a page in the web project named Readonly. aspx. The PageLoad code is as follows:
Protected void Page_Load (object sender, EventArgs e) {User user = Class1.user; Class1.user. name = "oec2005"; // correctly modify the value of the Application object in the static read-only field. oec2005 Class1.user = new User ("oec2005") is displayed on the running page "); // errors cannot be compiled and cannot be changed to another reference Response. write (user. name );}
As mentioned above, the value of a read-only field cannot be changed once defined. Even the reference type can only change the value of the referenced object. However, it is not so absolute. Let's take a look at how to use reflection to change the value of a read-only field.
1. Since Reflection is used, first reference the namespace using System. Reflection; and then modify the Class1 code.
namespace Oec2003ClassLibrary{ public class Class1 { public readonly Int32 _age = 25; public readonly User _user = new User("oec2003"); } public class User { public User(string name) { Name = name; } public string Name { get; set; } }}
2. modify the code in PageLoad and output the content of the read-only field before reflection.
Protected void Page_Load (object sender, EventArgs e) {Class1 myClass = new Class1 (); User user = myClass. _ user; Response. write ("name:" + user. name + "Age:" + myClass. _ age );}
The result is as follows:
3. Modify the PageLoad code, use reflection to modify the value of the read-only field, and then output the code.
Protected void Page_Load (object sender, EventArgs e) {Class1 myClass = new Class1 (); Type myType = typeof (Oec2003ClassLibrary. class1); myType. getField ("_ age "). setValue (myClass, 30); // change the value of the Value Type Read-Only field myType. getField ("_ user "). setValue (myClass, new User (""); // change the value of reference read-only field User user = myClass. _ user; Response. write ("name:" + user. name + "Age:" + myClass. _ age );}
The result is as follows:
It can be seen that both the Value Type Read-Only field and the reference type read-only field can be smoothly modified through reflection.
In some interviews, we often encounter the difference between const and readonly. In the final analysis, the base is the difference between constants and fields. The fields modified by readonly are only one type of fields, in actual applications, the choice of const or readonly should be based on actual needs. The performance of const is better, because there is no need to allocate memory, but there are many restrictions. For example, the type restrictions during definition are not very flexible.
Statement: This article Reprinted from http://www.cnblogs.com/oec2003/archive/2009/06/22/1508051.html