With quiet read CLR via C # (03)-object creation and type conversion

Source: Internet
Author: User
With quiet read CLR via C # (03)-object creation and type conversion

This section is not complex. It mainly introduces the process of creating a class instance and the knowledge of switching between types.

I, Create object

CLR requires the use of the new operator to create an object. The IL command generated by this operator during compilation is newobj. For example:

Student Xiaojing = new student ("Xiaojing", "1986 ");

InCreation processWhat happened?

    • Allocate space. Allocate the memory size required for this class in the managed heap.
    • Initialize an additional member of an object. Each object has two additional members: one is the pointer to the class method table, and the other is the syncblockindex member. CLR uses this field for thread synchronization control, some BITs can also be used as garbage collection tags. CLR manages object instances through these two members.
    • Call the constructor. The specified parameters can be passed in.
II, Type conversion

In C #, convert to the base class directly and implicitly. convert to the derived class requires display, because it may fail. At runtime, CLR checks the transformation operation to ensure that the object is converted to its actual type or its base type.

Class animal {}

Class dog: Animal {}

Animal A = new dog ();

Dog B = (DOG) A; // display conversion, base class to derived class

Animal A = new dog (); // implicit conversion, derived class to the base class

Is and?

There are two ways to check whether objects and classes are compatible: Is and.

    • The is keyword often needs to be converted twice in use. First, judge the type compatibility, and then always convert with a display.

If (A is dog) // The first conversion

{

Dog G = A; // Second Conversion

.........

}

    • The as keyword is converted once, and then you can determine whether the converted variable is null. Therefore, this method has relatively higher performance.

Dog G = A as dog; // convert once. If the conversion fails, the value is null.

If (G! = NULL ){...}

III, Instance Test

The following is an example of type conversion.CodeCan the code be correctly compiled and run? If you are correct, your understanding of this lesson will pass.

Or the two classes above, animal and dog, mainly examine the knowledge of type conversion.

Code

Correct

Compilation Error

Running Error

ClassProgram

   

{

     

Static void main (string [] ARGs)

     

{

     

Object O1 = new object ();

   

Object O2 = new animal ();

   

Object O3 = new dog ();

   

Object O4 = O3;

   

Animal animal1 = new animal ();

   

Animal animal = new dog ();

   

Dog dog1 = new dog ();

   

Animal anim_3 = new object ();

 

√ ①

 

Dog dog2 = new object ();

 

√ ②

 

Animal animal4 = dog1;

   

Dog dog3 = animal;

 

√ ③

 

Dog dog4 = (DOG) dog1;

   

Dog dog5 = (DOG) animal;

   

Dog dog6 = (DOG) animal1;

   

√ ④

Animal animal5 = (animal) O1;

   

√ ⑤

Animal animal6 = (DOG) animal;

   

}

     

}

     

 

Error point parsing:

① Animal anim_3 = new object ();

The base class should be converted to the derived class, so an error is returned when compiling. In fact, even if it is changed to display transformation, a runtime error will occur because the object type is incompatible. You can try to change it to animal anim_3 = (animal) O2;

② Dog dog2 = new object ();

The reason is the same as ①. The positive solution is dog dog2 = (DOG) O3;

③ Dog dog3 = animal;

The reason is the same as ①. The positive solution is dog dog3 = (DOG) animal;

④ Dog dog6 = (DOG) animal1

The base class shows the transformation to the derived class, so there is no syntax error, so the compilation passes. But at runtime, CLR checks the transformation operation to ensure that the object is converted to its actual type or its base type. The animal1 object is of the animal type rather than the dog type, so a transformation fails. If an animal1 = new dog () is added, then the transformation is successful.

⑤ Animal animal5 = (animal) O1;

The reason is the same as ④. I will not go into details here.

Are you correct?
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.