Why in the reference type, even if implicit conversion is done, the actual type conversion is not done even if explicit conversion is done?
While studying explicit and implicit transformations, I was wondering if this leads to real type transformations. So I created 2 classes and tested them as follows.
class Teacher : Person
{
public Teacher(int _age) : base(_age)
{
Age = _age;
}
}
class Person
{
public int Age { get; set; }
public Person(int _age)
{
Age = _age;
}
}
The output was all Teacher type. I checked that the implicit transformation in the value type also changed the actual type, but not in the reference type.
class Test
{
public static void Main()
{
Teacher teacher = new Teacher(20);
Person teacherToPerson = teacher;
Teacher TtoPtoT = (Teacher)teacherToPerson;
Person p844 = new Teacher(30);
Console.WriteLine(p844.GetType());
Console.WriteLine(teacherToPerson.GetType());
Console.WriteLine(TtoPtoT.GetType());
float floatNum = 4.5f;
double dNum = floatNum;
Console.WriteLine(dNum.GetType());
}
}
Does the original reference type not change the actual value type? Or is there something I missed?
JejuOrange is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1