Summary of common ways of C reference type conversions

  • 2020-11-03 22:34:12
  • OfStack

This article briefly describes, in the form of examples, some common ways to convert reference types, such as subclasses to superclasses, superclasses to subclasses, and transformations between relational classes that are not subparents. The following is for your reference:

1. Implicit conversion: converting a child class to a 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, the child class is converted to the parent class.


static void Main(string[] args)
{
  Dog dog = new Dog(2.5f,12);
  Animal animal = dog;
  Console.WriteLine(animal._age);
}

Results: 12

As you can see, the transformation of a subclass into a parent is an implicit transformation. This is done on the stack, with the subclass variable dog, then the superclass variable animal, and finally the heap address saved by dog assigned to anmial.

2. Strong transformation: The superclass is converted to a subclass

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)
  {
 Console.WriteLine(dog._age);
  }
  else
  {
 Console.WriteLine(" Conversion failure ");
  }
}

Result: An exception was thrown and Animal could not be converted to Dog

As you can see, if you use the above method to strongly convert a parent class to a child class, an exception will be thrown if the conversion fails.

3. as strong transform: superclass to subclass

If the client USES as to convert the parent class to the child class.


static void Main(string[] args)
{
  Animal animal = new Animal(12);
  Dog dog = animal as Dog;
  if (dog != null)
  {
 Console.WriteLine(dog._age);
  }
  else
  {
 Console.WriteLine(" Conversion failure ");
  }
}

Result: Conversion failed

As you can see, using as to strongly convert a parent class to a child class does not throw an exception if the conversion fails.

4. Use is to judge first and then enforce: the superclass is converted to the subclass

You can use is to determine whether the parent class can be converted to a subclass before you do a strong conversion, and then determine whether the subclass instance is null or not.


static void Main(string[] args)
{
  Animal animal = new Animal(12);
  Dog dog = null;
  if (animal is Dog)
  {
 dog = (Dog)animal;
  }
  if (dog == null)
  {
 Console.WriteLine(" Conversion failure ");
  }
  else
  {
 Console.WriteLine(" Conversion success ");
  }
}

5. Use the operator to achieve strong turn

You can design a static, implicit, operator method in one class to convert an instance of this class into another instance of a target transform object.


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 = benz.Mile};
    }
}
public class Car
{
public int Mile { get; set; }
}

The client


static void Main(string[] args)
{
  Benz benz = new Benz(1000);
  Car car = benz;
  Console.WriteLine(car.Mile);
  Console.ReadKey();
}

Results: 1000

The use of operators, the original no relationship between the two classes to establish a relationship, can achieve transformation
When Car car = benz is executed, the operator method operator Car of the Benz class is executed
Operator methods must satisfy several conditions: static, implicit, keep the name and the class name to be transformed by 1, and return the class instance to be transformed
When Car car = benz is executed, an instance of Car is created on the heap and assigned to the variable car on the stack

Conclusion:

(1) Subclass to superclass is an implicit conversion, the essence of which is that the value of one variable on the stack is assigned to another variable on the stack
(2) The parent class is converted to a child class, and it is easy to throw an exception if you use the "parent class instance" directly
The parent class is converted to a child class. If you use as, you can avoid throwing an exception
The parent class into and so on, also can use is judgment first, and then carry out the conversion
For the two classes that are not child parent relations, one operator method can be designed in one class to transform the class instance into the target object instance

I believe that this article has a certain reference value for the learning of C# programming.


Related articles: