Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I understand how polymorphism works. I understand how implicit and explicit castings work for reference types but I cannot understand how inherited methods that return parent's type work

For example:
C#
class SimpleClass
    {
        public SimpleClass ShallowCopy(SimpleClass sc)
        {
            return sc.MemberwiseClone();
        }
    }


This code won't work. I need to supply an explicit cast to the value being returned. But how does it work? The method "MemberwiseClone()" from the System.Object returns an object. So when it returns the object it's type should be only object? right? Then why am I able to explicitly cast the returned value to SimpleClass? How does this work? Could anybody explain this behavior to me?

What I have tried:

Scratching my head. Researching for more information in Microsoft Docs but there isn't anything like that explained there.
Posted
Updated 11-Mar-18 9:16am
Comments
Maciej Los 11-Mar-18 15:52pm    
Seems, you want to use [as] operator.

1 solution

Suppose you have three classes:
C#
class A {}
class B : A { public string S { get; set ; } }
class C : B { public string S2 { get; set ; } }

So C is derived from B which is derived from A.
That means that instances of both C & B are instances of A plus a bit, and that a C instance is also an instance of B. But that A is not a B or a C, and B is not a C.
A -> B -> C
So it's pretty obvious that you can assign an instance of B or C to a variable of type A:
A a = new B();
a = new C();
But you can't so it the other way around:
B b = new C(); // Fine.
b = new A();   // ERROR!
If you could, then you'd have problems when you tried to access b.S because an instance of A does not contain the variable at all.

With that - hopefully - clear, let's look at what you are doing.
return sc.MemberwiseClone();
MemberwiseClone returns an object because that is the base class for everything - all other classes implicitly derive from object. So when you return a SimpleClass you have to cast that object to your class or I could do this:
string LetSlipTheDogsOfWar(int i)
   {
   object o = i;  // perfectly legal, int is derived from object
   return o;
   }
...
string s = LetSlipTheDogsOfWar(666);  // Cry Havoc!

The system rightly complains that you are doing something very dangerous!
 
Share this answer
 
Comments
The_Unknown_Member 11-Mar-18 15:41pm    
Well that's the question exactly. the returned object is of type System.Object. The why is it possible to cast the returned System.Object object to SimpleClass? It doesn't make sense. System.Object isn't SimpleClass but SimpleClass is System.Object

Edit: Maybe the compiler does something behind the scenes and makes the derived MemberwiseClone() method return a SimpleClass object in the implementation? Isn't it the case? This behavior happens only when using the derived MemberwiseClone() method from the SimpleClass.
OriginalGriff 11-Mar-18 16:22pm    
Precisely! Simple class is an object - so it "fits" into an object variable. But at the same time, it's also a SimpleClass because it's a clone of a SimpleClass.
The MemberwiseClone method is inherited from object, so it can only return an object. To use it, you have to cast it back to the actual type - and if it isn't possible, you will get a run time error.

[Hah! Tablet autocorrect!]
Maciej Los 11-Mar-18 15:55pm    
Well explained!
This is a second time today when i post such of comment...

BTW: what you think about my comment to the question?
OriginalGriff 11-Mar-18 16:25pm    
TBH, I normally use "as", but in this case I'd use a "proper" cast specifically to get a runtime cat exception instead of returning a null value - it's clearer (to me at least) what the problem is that way.
BillWoodruff 12-Mar-18 12:25pm    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900