In C#, when the reference type needs to be converted, the keywords is, as and explicit strong conversion are often used. This article will experience the usage of these three.
Let’s first sort out the “conventional” or “convention” of .NET reference type conversion:
- Subclasses can be implicitly converted to parent/base class, which is what the "Rischer's substitution principle" says: subclasses must be able to replace their parent/base class.
- When converting the parent/base class to a subclass, explicit type strong rotation must be performed.
Convert subclass to parent class
class Program { static void Main(string[] args) { Animal animal = new Dog(){Name = "Dog",Age = 1}; (); (); } } public class Animal { public string Name { get; set; } } public class Dog : Animal { public int Age { get; set; } }
Output result: Dog
As mentioned above, you have experienced the conversion of subclasses to parent class. From this, you can see that subclass Dog can indeed replace parent class Animal. In other words, subclasses are parent classes. Instances of subclasses can be assigned to variables of parent class, without as or forced rotation, everything happens implicitly, which well reflects the "Rischer's replacement principle".
The parent class is converted to a subclass, and the parent class variable is created by subclass assignment
Parent class as subclass:
Animal animal = new Dog(){Name = "Dog",Age = 1}; Dog dog = animal as Dog; ( + " " + ); ();
Output result: Dog 1
The parent class explicitly forcefully converted to the child class
Animal animal = new Dog(){Name = "Dog",Age = 1}; Dog dog = (Dog)animal; ( + " " + ); ();
Output result: Dog 1
As above, whether using as or explicitly strong rotation, the parent class can be converted into a subclass, but there is a prerequisite: assign subclass instances to the parent class variable.
The parent class is converted to a child class, and the parent class variable is created through its own constructor
Failed to subclass parent class:
Animal animal = new Animal(){Name = "Sheep"}; Dog dog = animal as Dog; ( + " " + ); ();
Output result: Report "NullReferenceException" error
It can be seen that when the parent class variable is created through its own constructor, the parent class cannot be converted into a child class.
(1) In order to avoid the error of reporting "NullReferenceException" when as, introduce key is to make type judgment:
Animal animal = new Animal(){Name = "Sheep"}; if (animal is Dog) { Dog dog = animal as Dog; ( + " " + ); } else { ("animal cannot be converted to Dog"); } ();
Output result: animal cannot be converted to Dog
(2) When using as for type conversion, if the conversion fails, return null. Based on this point, you can also avoid error reports by judging whether the return value converted by as is null:
Animal animal = new Animal(){Name = "Sheep"}; Dog dog = animal as Dog; if (dog != null) { ( + " " + ); } else { ("animal cannot be converted to Dog"); } ();
Output result: animal cannot be converted to Dog
Failed to explicitly force conversion of parent class to subclass
Animal animal = new Animal(){Name = "Sheep"}; Dog dog = (Dog) animal; ( + " " + ); ();
Output result: Report "InvalidCastException" error
It can be seen that when the parent class variable is created through its own constructor, the parent class cannot be converted into a child class.
In order to avoid displaying the "InvalidCastException" error in the strong rotation time, introduce the keyword is for type judgment:
Animal animal = new Animal(){Name = "Sheep"}; if (animal is Dog) { Dog dog = (Dog)animal; ( + " " + ); } else { ("animal cannot be converted to Dog"); } ();
Output result: animal cannot be converted to Dog
Summarize
- For conversions of reference types, explicit strong rotation or as should be considered. The difference between the two is that once the type cannot be converted, an error is reported using an explicit strong transfer, while using as will return null.
- In order to avoid errors caused by explicit strong rotation or as, you should consider using is to determine whether the types can be converted.
- Using as for reference type conversion, you can not only use is to determine whether the types can be converted, but also determine whether the return value after as is null and then take corresponding operations.
- For conversions of basic types: use Convert, Parse, TryParse, etc.
is used to determine whether the type is consistent, as and explicitly strong conversion are used for type conversion.
This is all about this article on C# implementing reference type conversion. I hope it will be helpful to everyone's learning and I hope everyone will support me more.