Constant field method of Reading Notes in. NET Framework programming

Source: Internet
Author: User
Constant:
1. A symbol indicating a constant value
2. constants are generally considered as types rather than instances.
3. The value must be determined during compilation.
4. when the constant type is used, the compiler first looks for the symbol from the metadata of the module that defines the constant, directly retrieves the constant value, and then embeds it into the compiled il code. Therefore, A constant does not need to be allocated in memory at runtime, nor can it obtain a constant address. Nor can it transmit a constant as a reference.
5. Only when you are sure that the value of a symbol will never change will the other values be defined as constants.
6. constants can be defined only for primitive types. Boolean, Char, byte, sbyte, decimal, int16, uint16, int32, uint32, int64, uint64, single, double, and string. In addition, because the enumeration type is stored as a primitive type, it can also be used to define constants, although it is not a primitive type.
Public const int maxvalue = 100;

Field:
1. A field, also known as a data member, stores a value-type instance or a reference pointing to a reference type.
2. CLR supports two fields: type (static) and instance (non-static). For type fields, the system dynamically allocates memory when the type is loaded into an application domain; the instance field system dynamically allocates memory for instances of this type when they are built.

Method
1. When creating a reference type instance, the system first allocates memory for the instance, initializes the additional members of the object, and finally calls the instance constructor of the type to set the initial state of the object.
2. For the reference type, how many constructors will be initialized.

Class sometype
{
Int x = 5;
String S = "Hi, welcome to you ";
Double D = 3.1415926;
Byte B;

Public sometype (){};
Public sometype (int x ){.};
Public sometype (string s) {; D = 10 ;};
}
// The sometype-like instance will be initialized three times, and then D = 10 will be executed, because there are three Constructors

If you need to use many constructors, you should avoid initializing the fields when defining them and initializing them in the default constructor, then other constructors explicitly call this initialization constructor class sometype
{
Int X;
String S;
Double D;
Byte B;

Public sometype ()
{
X = 5;
S = "Hi, welcome to you ";
D = 3.1415;
}
Public sometype (int x): this () // The default constructor will be called here
{
This. x = X;
}
Public sometype (string S): this ()
{
This. S = s;
}
}

3. C # It is not allowed to define a no-argument constructor for a value type, but CLR does. Therefore, msil can be used to define no-argument constructor for the value type. Because the parameter-free constructor cannot be defined, the value type cannot be initialized (Inline) when defining fields ). Struct point
{
Public int X, Y;
Public point ()
{
X = y = 4;
}
}
Class rectangle
{
Public point left, right;
Public rectangle ()
{..;}
}
// In this case, the X and Y of the left right fields of the point will be initialized. Why? 0 or 4?

An error will be reported during compilation, because the non-argument constructor cannot be defined. If no parameter constructor exists, the value-type field is always set to 0 or null.
4. the verifiable Code requires that all value types be initialized before being read. Struct somevaltype
{
Int X, Y;
Public somevaltype (int x)
{
This. x = x; // y is not initialized.
}
}
// This part cannot be compiled, and somevaltype. Y is required to be assigned a value.

5. out ref
The use of the out and ref keywords for value-type parameters has the same behavior in a program as the transfer of value-type parameters. In the previous case, the out and ref keywords allow the called method to directly operate on a value-type instance. The calling code must allocate memory for the instance and be called to operate on the memory. In the latter case, the calling code is responsible for allocating memory for reference objects, the called method uses the passed reference (pointer) to operate the object.

Version of the virtual method:
1. vritual override
2. New eliminates the ambiguity of the method with the same name in the subclass and parent classes.
3. Subclass calls the method with the same name in the parent class using the base keyword

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.