Constants and fields in C #

Source: Internet
Author: User
Tags class manager

 

1. Constants

A constant is a special symbol that has an unchangeable value. When defining a constant, its value must be determined during compilation, after confirmation, the compiler saves the constant value to the metadata of the Assembly. Constants are always regarded as static members rather than instance members. Defining constants will lead to metadata creation.

This means that constants can only be defined for primitive types determined by the compiler. Then C # Allows defining a non-primitive constant variable, provided that its value is set to null.

Class Program

{

Public static const Program p = null;

Public static const Program p = new Program (); // compilation error. Only null can be used to initialize constant fields of the reference type (except strings ).

}

When the code references a constant symbol, the compiler searches for the symbol in the metadata of the Assembly that defines the constant, extracts the value of the constant, and embeds the value in the generated IL code, because the value of a constant is directly embedded in the code, no memory is allocated to the constant during runtime. Therefore, the constant address cannot be obtained, nor can the constant be transmitted as an address.

Therefore, constants do not have a good cross-assembly version control feature. constants are used only when the value of a symbol is determined to never change.

If you want to extract values from another assembly from one assembly at runtime, instead of using constants, you should use the readonly field.

2. Fields

A field is a data member that can hold a value type instance or reference a reference type.

CLR supports type fields and instance fields. For type fields, the dynamic memory used to hold field data is allocated in the type object. The type object is created when the type is loaded to an AppDomain. When will it be loaded to the AppDomain? This is usually the first JIT compilation when any method of this type is referenced. For instance fields, the dynamic memory used to hold field data is allocated when constructing an instance.

CLR supports readonly and read/write fields. Most of the fields are read/write, and the values of fields in code execution can be changed multiple times, however, the readonly field can only be written in one constructor method.

Internal class Manager

{

Public read only int d = 200;

Public Manager (int r)

{

// Modify the read field d. This is allowed because the code is in the constructor.

This. d = r;

}

}

Class Program

{

Public const Program p = null;

Static void Main ()

{

Manager m = new Manager (400 );

Console. WriteLine (m. d );//

}

When a field references a type and the field is marked as readonly, reference rather than the object referenced by the field cannot be changed:

Internal class Manager

{

Public static readonly int [] n = new int [] {22,33, 44,55, 66 };

Public read only int d = 200;

Public Manager (int r)

{

This. d = r;

}

}

Class Program

{

Public const Program p = null;

Static void Main ()

{

Manager. n [0] = 400;

Manager. n [1] = 800;

Foreach (int p in Manager. n)

{

Console. WriteLine (p );

} // Output 400,800, 66

}

}

 

 

Many fields are initialized inline (values are directly assigned to the Code for initialization, rather than through the constructor .) C # Allow this convenient inline initialization syntax to initialize constants and fields of the class. However, C # initializes the field in the constructor. The Inline initialization of the field is just a simplified syntax. When initializing a field in C #, if you use inline syntax instead of assigning values in the constructor, there are some performance issues to consider.

Author: in vain

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.