This article briefly describes several common ways of referencing type conversion in an example form, such as: subclass conversion to parent class, parent class conversion to child class, and conversion between classes that are not child parent-class relationships. The following are the descriptions for your reference:
1. Implicit conversion: subclasses are converted to parent class
public class Animal { public int _age; public Animal(int age) { this._age = age; } } public class Dog : Animal { public float _weight; public Dog(float weight, int age) : base(age) { _weight = weight; } }
Client, subclass is converted to parent class.
static void Main(string[] args) { Dog dog = new Dog(2.5f,12); Animal animal = dog; (animal._age); }
Results: 12
It can be seen that converting a subclass to a parent class is an implicit conversion. This conversion is completed on the stack. The variable dog represents the subclass, and then the variable animal represents the parent class. Finally, the heap address saved by the dog is assigned to the animal.
2. Strong transformation: convert the parent class into the child class
If the client parent class is converted to a child class.
static void Main(string[] args) { Animal animal = new Animal(12); Dog dog = (Dog)animal; Dog dog = animal as Dog; if (dog != null) { (dog._age); } else { ("Conversion failed"); } }
Result: Animal cannot be converted to Dog
It can be seen that if the above method is used to force the parent class to a child class, an exception will be thrown if the conversion fails.
3. Use as to force conversion: convert the parent class into a child class
If the client uses as to convert the parent class to a child class.
static void Main(string[] args) { Animal animal = new Animal(12); Dog dog = animal as Dog; if (dog != null) { (dog._age); } else { ("Conversion failed"); } }
Result: Conversion failed
It can be seen that using as to force the parent class to a child class, and the conversion failure will not throw an exception.
4. Use is to judge first and then force it to convert: the parent class to the child class
Before a strong turn, you can first use is to determine whether the parent class can be converted into a subclass, and then judge whether the conversion is successful based on whether the subclass instance is null.
static void Main(string[] args) { Animal animal = new Animal(12); Dog dog = null; if (animal is Dog) { dog = (Dog)animal; } if (dog == null) { ("Conversion failed"); } else { ("Conversion successful"); } }
5. Use operators to achieve strong rotation
You can design a static, implicit, operator method in one class to convert the instance of this class into another target conversion object instance.
public class Benz { public int Mile { get; set; } public Benz(int mile) { Mile = mile; } public static implicit operator Car(Benz benz) { return new Car(){Mile = }; } } public class Car { public int Mile { get; set; } }
Client
static void Main(string[] args) { Benz benz = new Benz(1000); Car car = benz; (); (); }
Results: 1000
○ Use operators to establish relationships between two classes that are not related to the original, and can achieve conversion.
○ When Car car = benz is executed, the operator method of Benz class operator Car will be executed
○ The operator method must meet several conditions: static, implicit, the name is consistent with the class name that needs to be converted, and return the class instance that needs to be converted.
○ When Car car = benz is executed, create a Car instance on the heap and assign it to the variable car on the stack.
Summarize:
① Converting a subclass to a parent class is an implicit conversion, and its essence is that a variable value on the stack is assigned to another variable on the stack.
② If the parent class is converted into a child class, it is easy to throw exceptions if the "(subclass) parent class instance" is used directly.
③ Convert the parent class to a child class. If using as, it can avoid throwing exceptions.
④ The parent class can be converted into something, and you can also use is to judge first, and then convert it
⑤ Two classes that are not child-parent relationships, you can design an operator method in 1 class to convert the instance of this class into a target object instance.
I believe that this article has certain reference value for everyone's learning C# programming.