Sunday, October 24, 2010

"CLR Via C #" Study Notes (3) constant and field



"CLR Via C #" book read twice before the rough, but has failed to understand in depth, but a lot of content is forgotten, is now ready to re-read it, and read the written part, because the process of writing is also a deeper understanding of the process. Study of this series be considered a record, it would not be easy after their inspection, if you still some help, I am very happy. I am a selective look at the book, so order and may not be the same as the order book.

CLR Via C # Study Notes (1) primitive type value type reference type

CLR Via C # Study Notes (2) boxing and unboxing

CLR Via C # Study Notes (3) constant and the field (cosnt readonly)

Constants and fields are the type of data members, but the difference is significant. See below under the constant example to understand

1. Constant value will never change. There are many types of fields, the value of non-read-only field can be changed.

2. Constant value must be determined at compile time, that the definition would assign. Compiled constant values on the preservation of the assembly metadata; field is stored in dynamic memory at run time to get field values.

3. Constant definition must be primitive types, the primitive types can refer to (CLR Via C # Study Notes (1) primitive type value type reference type); field definitions can be any type.

4. Because the constant value can not change, it can be seen as a static type, in the IL code, you can see there is constant static modifier, so call time and call the static field, as the class name directly. Constant names ; field before the call to the static fields and constants, like the class name directly. field name, call the non-static field must be an instance of the class. ,

5. C # does not allow modification of constant use of static, because the constant implied in itself is static type; field can use the static, use the static definition of the field is static fields.

1 Create a class library project named Oec2003ClassLibrary, in default of the class to write the following code in Class1

1 namespace Oec2003ClassLibrary

2 (

3 public class Class1

4 (

5 public const double PI = 3.14;

6

7 public static double _pi = 3.14;

8)

9)

10

2 Create a web project, add a reference to the Oec2003ClassLibrary class library project, the new page ConstTest.aspx, write the following code in the PageLoad

1 protected void Page_Load (object sender, EventArgs e)

2 (

3 Response.Write (Class1.PI + "

");

4 Response.Write (Class1._pi);

5)

3 will ConstTest Make start page, run, you can see the result as follows

4 will now Oec2003ClassLibrary project Class1's code to read as follows, and then re-compile the project.

1 namespace Oec2003ClassLibrary

2 (

3 public class Class1

4 (

5 public const double PI = 3.1415926;

6

7 public static double _pi = 3.1415926;

8)

9)

5, just refresh the page, you can see the following results

Page 6 of the new run ConstTest the following results can be seen

As can be seen from the above example, the application does not re-compile the case, the constant value will never change. If the application program wants to get a new constant value, Jiu must be re-compiled, so when an application program running set of applications that want to get another value of concentration, Ze Bu Neng use the constant, Ke Yi using the read-only Ziduan (readonly).

Field is also a type of data members, field modifiers are static readonly volatile, there is no modification of the Modified field is an instance of ordinary fields. static readonly can be used together, is a static read-only field. The volatile in the later part of the introduction.

About readonly must pay attention to

l 1 when the readonly field is modified by the value type, when the call to change the field if the value of view, it will compile error.

l 2 when the readonly field is modified with reference types, the call can not change its reference, but you can change the application object's value, see the following example.

1 projects in Oec2003ClassLibrary Class1.cs file to write the following code

1 namespace Oec2003ClassLibrary

2 (

3 public class Class1

4 (

5 public const double PI = 3.14;

6 public static readonly double _pi = 3.14;

7

8 public static readonly User user = new User ("oec2003");

9)

10 public class User

11 (

12 public User (string name)

13 (

14 Name = name;

15)

16 public string Name (get; set;)

17)

18)

19

2 In the web project, add pages, is named Readonly.aspx, PageLoad code below

1 protected void Page_Load (object sender, EventArgs e)

2 (

3 User user = Class1.user;

4 Class1.user.Name = "oec2005"; / / static read-only field is applied correctly modify the value of the object, run in the page display oec2005

5

6 Class1.user = new User ("oec2005"); / / error can not compile, can not change to another reference

7

8 Response.Write (user.Name);

9)

10

From the above you can know about the read-only (Readonly) field, once defined can not change its value, even if a reference type can only change the reference value of the object. However, it also is not as absolute, the following came to see how to use reflection to achieve change in the value of read-only field.

1 As used in reflection, the first reference to the namespace using System.Reflection;, and then modify the code in Class1.

1 namespace Oec2003ClassLibrary

2 (

3 public class Class1

4 (

5 public readonly Int32 _age = 25;

6 public readonly User _user = new User ("oec2003");

7)

8 public class User

9 (

10 public User (string name)

11 (

12 Name = name;

13)

14 public string Name (get; set;)

15)

16)

2 modified PageLoad code, will use reflection before the contents of the output read-only field.

1 protected void Page_Load (object sender, EventArgs e)

2 (

3 Class1 myClass = new Class1 ();

4

5 User user = myClass._user;

6

7 Response.Write ("Name:" + user.Name + "Age:" + myClass._age);

8)

The results are as follows

3 modified PageLoad code, using reflection and then modify the value of the output read-only field.

1 protected void Page_Load (object sender, EventArgs e)

2 (

3 Class1 myClass = new Class1 ();

4

5 Type myType = typeof (Oec2003ClassLibrary.Class1);

6 myType.GetField ("_age"). SetValue (myClass, 30); / / change the value type the value of read-only field

7 myType.GetField ("_user"). SetValue (myClass, new User ("glass")); / / change the reference value of read-only field

8

9 User user = myClass._user;

10 Response.Write ("Name:" + user.Name + "Age:" + myClass._age);

11)

The results are as follows

Oh, we can see whether it is read-only field value type or reference type read-only field can be successfully modified by reflection.

In some interviews often encounter the difference between const and readonly This problem of the root base is the difference between constant and fields, readonly field is modified by a field only, the practical application of the selection according to const or readonly actual demand, const better performance, because there is no allocation of memory, but many restrictions, such as restrictions on the type definition, seems not very flexible.







Recommended links:



Stop ekrn exe problems quick fixing tip



Hot Covert Surveillance



3G market to look FORWARD to



CTI Capital coveted



Picked Audio Recorders



ASF To MPG



The world's major MEDIA on the views of Chrome OS



Evaluate Java And JavaScript



Hot Graphic Viewers



MP4 To WMP



Great way to circumvent the procurement risk



MKV To WMV



QTP Descriptive language experience



For the B / S structure, to UNDERSTAND the point



FTP operation of COMMON sense



Robin Li Baidu executives leave: "make money the most important"



No comments:

Post a Comment