C # New and initialization

Source: Internet
Author: User

This article is only for individuals to learn, if there is a mistake, please correct me.

Although you know that you can create objects using new, you don't always understand the specifics of initialization and new knowledge.

Understand the relevant knowledge through 8 questions and needs.

Understanding Problems and requirements

Three steps of 1.new

2. What is the meaning of initialization?

3. The difference between a variable declaration and a variable assignment of NULL or a variable calling new.

4. The field is not a variable.

Three Steps for 1.new:

1.1 in the stack or heap to open up space, the size of the space determined by the class, the memory space point.

1.2 Creating objects in the open controls

Personal understanding the object of creation is to hold the field in the memory space, but the field does not point to the memory space where the data is stored, although the object occupies space in memory, but does not have a specific allocation.

1.3 for initialization.

2. Initialization:

Initialization is calling the constructor, assigning values to all fields, and assigning them to the default values.

Reference type is null.

The value type int is 0,double to 0.0.

Personal understanding: Let the field have the default value, after the allocation of space, memory is stored in the corresponding actual value, NULL means I did not make specific use, but I have the space to store data.

3. The difference between a variable declaration and a variable assignment of NULL or a variable calling new.

Distinguishes between a variable in a function and a field in an object primarily.

3.1 variable declaration, there is no meaning to assign value:

Declares an unassigned value and cannot be generated, prompting ' use of unassigned variables '.

By Reflecter you can see that declaring a variable is not assigned, and actually does not exist, it means that no memory has been allocated.

3.2 variable assignment is null

Indicates that the variable has memory space, but there is nothing in memory.

3.3 Variable Call new

There is a field in the variable, but the field is assigned and the assignment is the default value.

4. The field is not a variable.

Feel the difference between a variable and a field by 3 and 4.

A variable exists in a function where the field is a member of the class.

The function call field in the class, or the corresponding property, will only appear as null.

A function in a class declares a variable, and a situation occurs that cannot be compiled.

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.