Read the Classics-"CLR via C #" (Jeffrey Richter) Notes _new new objects

Source: Internet
Author: User

The CLR uses the new operator to create an object, for example:employee E=new Employee ("Param1"); Here's what the new operator does.

    1. It calculates the number of bytes required for all instance fields defined in the type and all of its base types (until System.Object, although it does not define its own instance field). Each object on the heap requires some extra members-that is, the type object pointer and the sync block index. These members are used by the CLR to manage objects. The number of bytes for these extra members is counted into the object size.

    2. It allocates the memory of the object by allocating the number of bytes required by the specified type from the managed heap, and all allocated bytes are set to 0 (0).

    3. It initializes the object's "type Object pointer" and "synchronous Block index" members.

    4. Invokes the instance constructor of the type, remembering any arguments passed in the call to new (The string "Param1"is the example above). Most compilers automatically generate code in the constructor to invoke a base class constructor. Each type of constructor, when called, is responsible for initializing the instance fields defined by this type. The final call is the System.Object Constructor, which simply returns and does nothing else. To prove this, you can use ILDasm.exe to load MSCorLib.dll and examine the constructor method of System.Object yourself.

When new executes all of these operations, it returns a reference (or pointer) to the new object. In the preceding example code, the reference is saved to the variable E, which has the Employee type.

Read Classics-CLR via C # (Jeffrey Richter) Notes _new new objects

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.