C # Constructor's understanding (2)

Source: Internet
Author: User

The previous article introduced the constructor of the reference type.

The focus of this article is on the Value Type constructor.

The following describes the constructor of the Value Type:

Let me first start with my initial impression on the Value Type constructor.

1. Generally, no constructor is required for the value type. CLR ensures that each field is 0 or null and initializes each field.

2. There can be constructor for the value type, but it must be a constructor with parameters and must be explicitly called. No can be called.

3. If the value type does not have a constructor, the CLR ensures that each field in the value type is initialized to 0 or null. However, once the constructor is in place, the CLR does not support initializing each field, you can only initialize the initial values of each field in the constructor.

4. the CLR ensures that the initial value is null or 0 only when the value type is nested in the class. If the value type is a value type on the stack, it cannot be initialized to 0 or null.

First, let's take a look at several common errors.

Struct point {int x = 5; // Why are these errors? This is a simple syntax to facilitate fields initialized by the non-argument constructor, because the value type does not allow the presence or absence of constructors, this is an incorrect int y ;}

Struct point {// int x = 5; // Why are these errors? This is a simple syntax to facilitate fields initialized by the non-argument constructor, because the value type does not allow the presence or absence of constructor, this is an incorrect int X; int y; // point () {} cannot contain any parameter constructor. The point (int I) is incorrect. {// If a constructor is available, the CLR does not guarantee that each field can be initialized to 0 or null, // ensure that each field is initialized to 0 or null within the value type. // This is because before any field of the value type is called, each field must have an initial value. X = I; // This method is incorrect. X = y = I; // This method is correct. Or assign y to 0 ;}}

If you are really interested in this, you need to do more experiments on your own. Today, I am going to study the multi-value constructor, I will study it in depth later.

The code here is all the code I learned

Using system; using system. data; using system. text; namespace mystu {/* Value Type constructor */class programe {static void main (string [] Arg) {try {baseclass OBJ = new baseclass (); obj. test ();} catch (exception ERR) {console. writeline (err. message) ;}}sealed class baseclass {public void test () {console. writeline ("base class constructor") ;}} struct point {// int x = 5; // Why are these errors: this syntax is used to facilitate the initialization of fields in the non-argument constructor. Because the value type does not allow any parameter constructor In this case, the int X; int y; // point () {} cannot contain any parameter. The constructor point (int I) {// If a constructor exists, therefore, CLR does not guarantee that each field can be initialized to 0 or null. // all fields within the value type must be initialized to 0 or this null. // This is because before any field of the value type is called, each field must have an initial value. X = I; // This method is incorrect. X = y = I; // This method is correct .}}}

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.