2

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?

4
  • 1
    Yes this is expected behaviour. Reference conversions don't actually change anything at runtime. What do you mean by "why"? Commented May 22, 2024 at 7:44
  • What is a "real type tranformation" for you? A new instance of a new type that has no relation to the original? Then you need to create a new instance. Commented May 22, 2024 at 7:47
  • You should have a look at this question, even though it's not a complete answer: stackoverflow.com/questions/1584293/… Commented May 22, 2024 at 7:50
  • All the actual objects you created are of the Teacher class. Converting their reference to a base class (Person) does not change that (A Person variable can reference a Teacher instance. Polymorphism is based on that). Commented May 22, 2024 at 7:50

1 Answer 1

4

There are no implicit or explicit "transformations" (conversions) shown in the code relating to Teacher or Person, nor can there be (it isn't valid to define such a conversion operator); this is a reference cast operation, which is defined as changing the considered type of a reference (to provide access to the API specific to a particular level of the type hierarchy), without changing anything at all about the underlying object that the reference points to (note: changing to a more specific type may involve a test that the operation is valid, i.e. if you try to change a Person reference to a Teacher reference, the system will usually try to check that the person actually is a teacher, else throw an InvalidCastException).

In the case of float to double: there exists a system-defined conversion between these types. When two types are not in the same hierarchy, you can define your own conversion operators, but IIRC in this specific case they are defined in the language/runtime specification rather than as regular operators in the type system.

Sign up to request clarification or add additional context in comments.

6 Comments

There are no implicit or explicit "transformations", but there are implicit or explicit reference conversions, so I think it's inappropriate to annotate transformations with conversions.
@shingo I'm not entirely sure how to parse "annotate transformations with conversions", but ultimately there's a risk of getting bogged down in word choices, when it isn't the words that matter: it is what happens
This is not word choices, now your first sentence can be read as "There are no implicit conversions shown in the code relating to Teacher or Person", however Person p844 = new Teacher(30); this is definitely an implicit conversion.
@shingo sigh, here we go, debating irrelevant word choices; the intent here was to emphasize "transformation" meaning a change in the actual thing (object/value/etc), which the OP hints at with "real type transformations" in the question. But to emphasize: I have zero interest in the minutiae of naming in this context; what matters is how things behave
@shingo It's an implicit cast not an implicit conversion. The C# spec calls that a "reference conversion" learn.microsoft.com/en-us/dotnet/csharp/language-reference/… moreover "Reference conversions, implicit or explicit, never change the referential identity of the object being converted. Note: In other words, while a reference conversion can change the type of the reference, it never changes the type or value of the object being referred to. end note"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.