C # value type instance Builder _c# Tutorial

Source: Internet
Author: User
A reference type contains a value type field that, when initialized by a reference type, is initialized to 0 and Null by default.
The CLR allows constructors to be defined for value types, but calls to constructors must explicitly write code to invoke them.

The CLR does not allow the definition of a parameterless constructor for a value type. You can only define a parameter constructor, and you must assign a value to all the fields in the value type in the constructor, or you will be given an error.

Although there is no parameterless constructor in C #, you can use this syntax to initialize the fields that are inside:

Structtype st = new Structtype ()//Initialize 0 or null for the inner field

So the reference to the definition of a parametric constructor is:
Copy Code code as follows:

public structtype (int x) {//definition of a parameter constructor
this = new Structtype ()//Initializes all fields to 0 or null
m_x = x;//overwrite m_x field with parameter X, m_x has been initialized to 0 by the previous sentence
}

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.