"Clr.via.c# Third Edition" Part II 4th, Chapter 5 reading notes (ii)

Source: Internet
Author: User

These two chapters are all theoretical, and I don't think I need to be too much of a blind alley. The theory of this thing, only in the long-term practice to slowly understand will be profound. I'll just write down some key knowledge that I think is important.

(i) Type conversion

Knowledge Points: Conversions to a base type are considered to be a safe, implicit conversion; When you convert to a derived type, only transformations are displayed.

Example:

New= (Employee) o;

Important cognition: The CLR's type checking iterates through the inheritance hierarchy and checks the specified type with each base type.

Common code: (see Code snippet below.) The CLR checks the type of objects two times):

if  is Employee) {      = (Employee) o;            }

Simplify the code: (see Code snippet below.) The CLR checks the type of the object at a time)

 as Employee; if NULL ){     //...}

(ii) value types and reference types

Important cognition: 1. The stack stores a value type, a pointer to a reference type (address), and a heap in which the reference type itself is stored (not all, see 2).

2. Value types that are contained within a reference type are not stored in the stack, but are in the heap (or value types, not boxed) and are included in the reference type object.

(iii) Attention to the impact of boxing and unpacking on program performance

Understand the three-time boxing in the following code:

 Public Static void Main () {      Int32 v=5;       =V;      V=123;       "  " + (Int32) o);}

The first boxing is to convert V to object; the second and third is because the WriteLine () method takes a string object so that V and the unpacking o are re-boxed.

What happens in "boxing":

1. Allocating memory in the managed heap, including the amount of memory required by the value Type field + The amount of memory for the type object pointer + memory required for the synchronization block index.

2. The value type of the field is copied to the newly allocated heap memory.

3. Returns the address of the object. (Address is a reference (pointer) to an object, and a value type becomes a reference type)

Concept application:

Override the ToString () method in the class to avoid boxing problems when using the ToString () method.

 Public class a{      privateint  x;        Public Override String ToString ()      {            returnstring. Formart ("{0}", x);}      }

Note Override the ToString () method internally if the base is called. ToString (); When this method is called externally, the value type is still boxed.

"Clr.via.c# Third Edition" Part II 4th, Chapter 5 reading notes (ii)

Related Article

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.