Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Recasting" a subclass instance (declared as base class) to an interface - why does it work?

I want to have subclasses that implement arbitrary interfaces, but are declared as a base class, so that the application would try to "recast" the instances to that interface and use them if recast is successful.

I had some doubts and so I made this short spike and, both for my joy and surprise, it works.

My question is: Why and how does it work? Is there a formal way that I could have known it would work beforehand? Some sort of "casting rules" in C#?

class Program
{
    static void Main(string[] args)
    {
        var obj = new SubClass() as BaseClass;

        Console.WriteLine(obj is ISubClass); // output = "True"

        Console.ReadKey();
    }
}

class BaseClass { }

class SubClass : BaseClass, ISubClass { }

interface ISubClass { }
like image 449
heltonbiker Avatar asked Mar 04 '26 16:03

heltonbiker


2 Answers

You have created an instance of SubClass and SubClass is inheriting from both the interface and BaseClass, so we can cast the reference of the object SubClass to either of those. The actual type still remains SubClass, you have just casted the reference to it's base type, if you check by calling GetType() on the instance you will see that it will evaluate to type SubClass, so it is just reference conversion, the original object still is same.

You might want to read about polymorphsim and Reference Conversion


(edit by OP) From second link:

Conversions from a base type to a derived type only succeed at run time if the value being converted is a null reference or a reference type that is either the derived type itself or a more derived type.

(...) a class type can always be cast to an interface type that it implements. Similarly, conversions from an interface type to a class type that implements it only succeed at run time if the value being converted is a null reference or a reference type that is either the class type itself or a type derived from the class type.

like image 95
Ehsan Sajjad Avatar answered Mar 06 '26 07:03

Ehsan Sajjad


The object is still of type SubClass you only use as reference the type of BaseClass. And because it is of type SubClass it also does implement the ISubClass interface. A cast does not update the origin object, it does only "change the reference" to the object.

Like you show in your code, with simple changes:

  1. you create a SubClass object: SubClass obj = new SubClass();
  2. now you cast it: BaseClass bObj = obj as BaseClass; but the original object is still of type SubClass. You could also do this ISubClass sObj = obj as ISubClass; and ISubClass sObj = bObj as ISubClass because you work always on the origin object.

Another example is polymorphism. The whole concept would not work, when the origin object would be changed when casting.

  • As general hint, you do not need the explicite cast to var obj = new SubClass() as BaseClass;, this would also work: BaseClass obj = new SubClass();
like image 32
Kevin Wallis Avatar answered Mar 06 '26 05:03

Kevin Wallis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!