The 7th Chapter constants and Fields

Source: Internet
Author: User

7.1 Constants

A constant (constant) is a special symbol that has a value that never changes. Constants can only be initialized in declarations. When you define a constant symbol, its value must be determined at compile time.

A constant expression is an expression that can be fully evaluated at compile-time. Therefore, constants cannot be initialized from values that are extracted from a variable.

If the const int a = B+1;B is a variable, it is obvious that the result cannot be computed at compile time, so constants cannot be initialized with variables.

Once determined, the compiler saves the value of the constant to the assembly's metadata. This means that you can define constants only for the primitive types that the compiler determines. However, C # also allows you to define a non-primitive type of constant volume (constant variable), provided that its value is set to NULL.

class Calendar1        {            publicconstint;        }

When the code refers to a constant symbol, the compiler looks for the symbol in the metadata of the assembly that defines the constant, extracting the                                                                                                                                                                                                                        the value of the constant and embeds the value in the generated IL code. Because the value of a constant is embedded directly into the code, you do not need to allocate any memory for the constant at run time.

Constants are immutable values that are known at compile time and do not change during the lifetime of the program. Constants are declared using the const modifier.

Only C # built-in types can be declared as Const. User-defined types (classes, structs, and arrays) cannot be const. Use the ReadOnly modifier to create classes, structs, and arrays that cannot be changed at run time.

You can use enumeration types to define named constants for integer built-in types (columns such as int, long, and so on).

When the compiler encounters a constant modifier in C # source code, the literal value is replaced directly in the intermediate language (IL) code it generates. Because there is no variable address associated with a constant at run time, the Const field cannot be passed by reference.

Constants can be marked as public, private, protected, internal, or protectedinternal.

An expression that is not contained in a class that defines a constant must use the class name, a period, and a constant name to access the constant. For example:

int birthstones = calendar.months;

Because constant values never change, constants are always treated as static members, not instance members.

Constants do not have a good cross-procedural versioning feature. After the developer has changed the value of the constant, the application has to get a new constant value that must be recompiled. If you extract the value of another assembly from one assembly at run time, you should not use constants, but you should use the ReadOnly field.

7.2 Fields

A field is a data member that holds an instance of a value type or a reference to a reference type.

A field is any type of variable that is declared directly in a class or struct.

Field Modifiers:

V Static: Is part of the type State, not part of the object state. This allows the calling method to use the field at all times, even if the class does not have any instances. No matter how many objects or no objects are generated by the class that contains the static field, there is only one instance of the field, and the static field cannot be undone. You must refer to the static field in the following way: Class name. static field name.

V ReadOnly: A read-only field can be assigned only during initialization (in the field Declaration) or in the constructor of the definition class (this constructor method can only be called once, that is, when the object is first created), and the value of the read-only field cannot be changed anywhere else. Note: However, you can use reflection to modify the ReadOnly field.

The CLR supports type (static) and instance (non-static) fields. For type fields, the dynamic memory that holds the field data is allocated in the type object, and the type object is created when the type is loaded into an AppDomain. For instance fields, the dynamic memory used to hold the field data is allocated when an instance of the type is constructed.

The AppDomain simply means the boundary of the application. It can be re-quarantined in the same process through it. When a program runs, it forms a program domain with a collection of all the modules it references and reflects. Ordinary desktop program, a program is an AppDomain. The CLR allows multiple programs to be hosted in one process (such as an IIS-like program), and one IIS can run many Web sites, and if the sites are in an AppDomain, one site crashes and other sites are inaccessible. If each site is a standalone program, the performance requirements for the machine are too high and there is no way to share resources. So. NET has the concept of an AppDomain, an IIS process that gives each site an AppDomain, each of which is independent of each other.

Because the fields are stored in dynamic memory, their values are available at run time. A field can be any data type, not necessarily just a compiler built-in primitive type as a constant.

The 7th Chapter constants and Fields

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.