Instance constructor, value type, and reference type

Source: Internet
Author: User

Instance constructor, value type, and reference type

 

The constructor name is the same as the class name.

1. Instance constructor and class (reference type)

The constructor is a special method that allows you to initialize the type strength to a good state.

When creating an instance of the reference type (class), first allocate memory to the data field of the instance, and then initialize the additional fields of the object (the type object pointer and synchronization index are fast ), finally, the instance constructor of the type is called to set the initial state of the object.

What happens to a new class: Memory Allocation (hosted on the stack) -- Additional overhead of initialization (fast type object pointers and synchronized indexes) -- Strength constructor of the call type (set the error status of the object)

When constructing an object of the reference type, before calling the instance constructor of the type, the memory allocated to the object is always classified as zero. The constructor does not display a value of 0 or null for all overwritten fields.

The instance constructor can never be inherited, that is, the class only has the instance constructor defined by the class itself.

Because it cannot inherit from the instance constructor, virtual, new, override, sealed, and abstract cannot be used in the instance constructor.

If no constructor is defined for the defined class, the c # compiler defines a (No parameter) constructor that calls the base class by default.

One type can define multiple instance constructors. Each constructor must have different signatures (parameters), and each constructor can have different accessibility.

Before accessing any fields inherited from the base class, you must call the base class constructor.

Internal sealed class SomeType {private int a = 5; private string B = "helllo"; private double c = 3.14; private Boolean d = false; // The following are some constructors // public SomeType () {}// private SomeType (int aa) {a = 10 ;}// protected SomeType (string bb) {}// internal SomeType (double cc) {c = 1.41;} // public SomeType (Boolean dd) {a = 1; B = "HI"; c = 1.73; d = true;} // The constructor sets all fields as default values, and the constructor calls the constructor public SomeType () {a = 10 in real time; B = "HI"; c = 1.41; d = false;} // set all fields to the default value. modify a private SomeType (int x): this () {a = 5 ;}
// Set all fields as default. Modify a and d.
public SomeType(int x, Boolean y) : this() { a = 5;d = true; } }

2. instance constructor and Value Type

CLR always allows the creation of value type instances, and there is no way to prevent Value Type instantiation. Therefore, you do not need to define the constructor for the value type.

C # The Compiler does not generate a default no-argument constructor for the value type.

The Value Type constructor is executed only when the call is displayed.

The parameter-free constructor of the value type definition is not compiled by c. No parameter constructor. The value type fields are always initialized to 0 or null.

Any stack-based value type fields must be written before being read; otherwise, security vulnerabilities may occur.

Assign values to all fields before accessing any field of the value type. Therefore, all fields of the value type must be initialized in Any constructor.

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.