C # difference between the reference type Customer customer1; customer1 = new Customer (); and Customer custumer1 = new Customer ();

Source: Internet
Author: User

The allocation of reference types is quite complex. This blog post only describes the differences between the two instantiation methods.

Static void Main (string [] args) {Customer customer1; customer1 = new Customer ();}

Customer customer1;

Declare a Customer reference customer1 and allocate storage space to this reference on the stack. However, this is only a reference, not the actual Customer object. customer1 reference occupies 4 bytes of space (GetHashCode () returns an int type ).

Customer1 = new Customer ();

This line of code completes the following operations: First, it allocates memory to store the Customer object (a real object, not just an address ). Then, set the value of customer1 to the internal address assigned to the new Customer object (it will call the fields in the initialization class of the currently called constructor ). The Customer instance is not stored in the stack, but in the heap. Assuming that the current new Customer () object occupies 32 bytes, the. NET Runtime Library selects an unused, 32-byte continuous block in the heap.

Static void Main (string [] args) {Customer customer1 = new Customer ();}

This operation merges the preceding two steps into one step for execution.

Another problem is:

Static void Main (string [] args) {Customer customer1; Customer customer1 = null ;}

What are the differences between the two methods.

 

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.