Therefore, I don’t have deep knowledge about how data is stored in the .NET Framework in terms of user types, but I was looking for an explanation of how the casting system works.
For example, if you need to do an explicit conversion from ValueType Struct as Char to Byte as follows:
char C = '#'; byte B = (byte)C; Console.WriteLine(B.GetType());
I was told that B is a byte that makes sense.
Now let's say that I selected from the Custom Class Flower to my base class Plant, why the output shows the derived class, regardless of how it is executed as follows:
class Plant{} class Flower:Plant{}
.
Flower Rose = new Flower(); Plant RoseBush = (Plant)Rose; Console.WriteLine(RoseBush.GetType()); //Outputs Flower Plant Rose = new Flower(); Plant RoseBush = (Plant)Rose; Console.WriteLine(RoseBush.GetType()); //Outputs Flower as well
I assume my question is why does the type not show the current type of the custom type, which is Plant, and why is this different from the value types at the beginning?
In addition, why do two examples with a flower and plants, although written differently on the first line, display the same thing?
source share