CLR. via. C # Part 2, Chapter 4, 5, Reading Notes (II ),

Source: Internet
Author: User

CLR. via. C # Part 2, Chapter 4, 5, Reading Notes (II ),

These two chapters are all theoretical and I don't think it's necessary to be too arrogant. The theory is profound only when it is learned in long-term practice. Below I will only write some key knowledge that I think is important.

(1) type conversion

Knowledge Point: conversion to the base type is considered to be a safe implicit conversion. Only conversions can be displayed during conversion to the derived type.

Example:

Object o = new Employee();Employee e = (Employee)o;

Important Cognition: the CLR type check traverses the inheritance hierarchy and uses each base type to check the specified type.

Common Code: (see the code segment below. CLR checks the object type twice ):

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

Simplified Code: (see the code snippet below. CLR checks the object type once)

Employee e = o as Employee;if(e != null){     //...}

(2) value type and reference type

Important Cognition: 1. The value type and reference type pointer (Address) are stored in the stack, and the reference type itself is stored in the stack (incomplete, see Figure 2 ).

2. The value type included in the reference type is not stored in the stack, but in the heap (or value type, not boxed). It is included in the reference type object.

(3) attach importance to the impact of packing and unpacking on Program Performance

Understand the three-way packing in the following code:

public static void Main(){      Int32 v=5;      Object o =v;      v=123;      Console.WriteLine(v + ", " + (Int32)o );}

The first packing is to convert v to an object. The second and third packing is because the WriteLine () method needs to obtain a String object, so that v and the split o will be packed again.

"Packing:

1. allocate memory in the managed heap, including the amount of memory required for value-type fields, the amount of memory required for type object pointers, and the amount of memory required for synchronizing block indexes.

2. Copy the value type field to the newly allocated heap memory.

3. Return the address of the object. (The address is a reference to the object (pointer), and the value type changes to the reference type)

Concept application:

Override the ToString () method in the class to avoid the packing problem when using the ToString () method.

public class A{      private int x;      public override String ToString()      {            return string.formart("{0}",x);      }}

Note that if base. ToString () is called internally in the overwritten ToString () method, the value type is still boxed when this method is called externally.


What about clr via c?

For the first time, I felt very difficult, but it was not very difficult to read my head. I can learn more from this book. If I use Keni, this is an entry-level book, because after reading it, you will have a lot of questions with these ......

CLR via C # Can I copy a third-edition ebook? Email: 10139713 @ qqcom

This is the book. You need it.

CLRviaC # Author: (US) Number of pages: 778 published on: 2010.09 Introduction: This book targets CLR and. NET Framework 4.0 is deeply and comprehensively discussed. It also introduces how to use them for design, development, and debugging. Chapter 29, Part 5. Part I introduces CLR basics, Part II explains how to design types, Part III...
 

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.