Type Basics of the first read CLR Via C # (1)

Source: Internet
Author: User

 

Recently, due to business trips and new knowledge, I have not read this book, nor have I updated the subsequent content of this series. However, this book is indeed a good book, I didn't want to let it go. Today, it happened to be a leisurely day. So today I reviewed my previous content and started reading new things. Update as soon as possible today.

Before starting, I want to talk about the concept of inheritance. As we all know, object orientation is embodied by encapsulation, inheritance, and polymorphism. If you are not clear about encapsulation, go to Google to find the answer. So what is inheritance? "Inheritance" can be understood as the ability to expand the class without re-writing the class itself, and all the functions of the existing class can be used. The new class created through inheritance can be called "subclass" or "derived class". On the contrary, the inherited class is called "parent class" or "base class ", A subclass can inherit multiple parent classes (a derived class can inherit multiple base classes). However, a subclass can only have one parent class, and multiple inheritance is required, it can be achieved through multi-level inheritance. I will briefly introduce the concept of inheritance here. If you still have some readers who do not understand it, you can find some information on the Internet, I believe that there are also blog posts dedicated to the concept of inheritance in the blog Park.

In C # or JAVA, no matter whether we create or call any class, its top-level parent class only has the class System. Object. That is to say, all classes are derived through it. When we create a class, no methods, fields, or attributes in the class are written. After being instantiated in another class, some methods such as Equals () are displayed (); getHashCode (); ToString (); GetType. These methods are the methods in the System. Object Class. This is an inherited concept. When we create a class, it inherits System. object class, with System. methods In the Object class, and. the Object class is extended. I mentioned an instantiation above. We all use the new keyword to instantiate a class (non-static class (static). When we instantiate a new class, A "Type object pointer" and "fast synchronization index" will be automatically created on the heap, And the CLR will automatically allocate the memory required for this class, the memory size is set based on the number of bytes required by the fields in the class. However, the new keyword will call System. object constructor and return it. A reference (Object Pointer) is returned to the new Object. When we call methods and attributes in the class, the operation is performed based on the referenced memory address. It can also be understood that we operate on the memory (actually the CLR helps us). Here there may be a problem, that is, how to release the memory of this object. This is the turn of the CLR garbage collection mechanism (GC) to detect and automatically release memory that is no longer accessed. The garbage collection mechanism will be updated after the basic content is updated.

In our daily use, type conversion is frequently used. Can the type be converted at Will (for example, the Person type is converted to the DateTime type )? The answer is no. But why not? First, security is one of the most important features of CLR. This security has restricted us. The GetType () method is used when reflection is used. This method is a non-virtual method and cannot be rewritten. This method cannot be rewritten, this means that we cannot disguise the Person class as a DateTime type through the GetType method and return it. During type conversion, CLR allows us to forcibly convert a parent class to its subclass, without any special syntax to convert the Child class to its parent class. For example:

Namespace Chapter4

{

Class Program

{

Static void Main (string [] args)

{

// No forced conversion is required for converting subclass to parent class

Object o = new Person ();

// Forced type conversion is required for converting the parent class to a subclass.

Person p = (Person) o;

}

}

 

// This class inherits from the System. Object Class

Internal class Person

{

Private string name;

Private int age;

}

}

 

So why cannot I convert the two classes from the Object class? During each conversion, the CLR checks the converted object to determine whether o references the Peron object or whether it is any subclass derived from the Peron class, if the CLR permits conversion, otherwise an exception will be thrown. For example, convert Peron to DateTime. The Code is as follows:

Class Program

{

Static void Main (string [] args)

{

// No forced conversion is required for converting subclass to parent class

Object o = new Person ();

// Forced type conversion is required for converting the parent class to a subclass.

Person p = (Person) o;

// Convert the Child class of Person into Person

Man m = new Man ();

MagicPerson (m );

// Construct a DateTime object and convert it to Person

DateTime dtNow = DateTime. Now;

MagicPerson (dtNow );

}

 

Private static void MagicPerson (object o)

{

Person p = (Person) o;

}

}

 

// This class inherits from the System. Object Class

Internal class Person

{

Private string name;

Private int age;

}

// This class is inherited from Person (the highest base class is System. Object)

Internal class Man: Person

{

Private string sex;

}

 

The above code can be compiled and run. When the MagicPerson method is run for the second time, the CLR will remove an exception: System. InvalidCastExiception and tell us that the conversion fails. Why does conversion fail? Let's analyze the workflow of CLR type conversion. First, during compilation, the compiler does not know who references the parameter o in MagicPerson, so the compiler allows the code to be compiled. However, at runtime, CLR knows that the first input of o is a reference of Man and inherits from the Person class. The first conversion is successful. When the MagicPerson method is entered again, the reference of the object is DateTime. CLR checks whether the DateTime type is inherited from Person Or Peron. If the Person type is not inherited from the Person type, an exception is thrown to terminate the transformation.

In the above code example, the MagicPerson method parameter should be set to the Person type in actual use. In this way, the compiler will report an error, before causing greater losses, put the seeds of evil in the cradle.

Go to dinner first, and update after dinner.

 

 

Author: LouisLee

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.