Casting calls different gettypes

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()); // Outputs "Byte" 

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?

+5
source share
2 answers

The result of the first actor is a different meaning. Now he is byte not a char .

The second listing is a reference conversion. The result is the same set of bits - a reference to the same object - only with a different type of compilation time. GetType() returns the actual runtime type of the object, which is the same in both cases (because it is the same object).

This can be seen in your example:

 // Variable names modified to follow normal conventions Flower rose = new Flower(); Plant roseBush = (Plant) rose; Console.WriteLine(ReferenceEquals(rose, roseBush)); // True 

Impacts for reference types can lead to different meanings if you are not user transformations, but these are not “reference transformations” in the language terminology. For instance:

 XElement element = new XElement("name", "value"); string value = (string) element; 

Here, value really not a reference to the same object as element - it is a reference to a string object instead of an XElement object, through an explicit conversion to string defined by XElement .

+8
source

The C # cast syntax does different things in different cases. When you talk about a class and a base class / subclass / interface, it’s backing up links - all you really do is type checking (and even this only happens if the compiler does not know what it should work - it omitted when issuing a subtype to the base type, for example). In this case, the only thing that changes is the type that the / JIT compiler is thinking about.

However, there are also type conversions. They can be embedded (as is the case with primitives such as byte / char ), or they can be provided as custom conversion operators (using the implicit or explicit keywords). In this case, it depends on the code what is happening (it could be anything).

So: the difference here is that it is a built-in primitive conversion, and one is type checking while maintaining the link.

+2
source

Source: https://habr.com/ru/post/1269785/


All Articles