Question

How exactly does this work?

If I have this base class

public class BaseClass
{
    public int Value1 { get; set; }
    public int Value2 { get; set; }

    public BaseClass SimpleClone()
    {
        BaseClass result = new BaseClass();
        result.Value1 = this.Value1;
        result.Value2 = this.Value2;
        return result;
    }
}

And this child class

public class DerivedClass : BaseClass
{
    public bool Value3 { get; set; }

    public DerivedClass()
    {
        Value3 = true;
    }
}

How can I downcast BaseCast.SimpleClone() into anotherObj? What would happen to Value3? While knowing what happens is good, I am also interested in why it works this way.

Was it helpful?

Solution 2

The question as it stands does not make sense. There is no possibility for downcasting, because your clone method already returns the base class. What I think you do (should) want here is override the clone method in your subclass:

public class DerivedClass : BaseClass
{
    //... Other stuff
    public BaseClass SimpleClone()
    {
        var result = new DerivedClass 
                         {
                             Value1 = this.Value1,
                             Value2 = this.Value2,
                             Value3 = this.Value3,
                         }
        return result;
    }

Now, if you have an object of type DerivedClass and call clone on it, the compiler does not know that the returned object is of type DerivedClass, because of the method signature. But you do, so in that case you can cast the object. All it does is tell the compiler: 'I know better, you think this is a BaseClass but it actually is a DerivedClass'. It has no runtime impact because you don't change the type of the object, you only provide the compiler with extra information.

OTHER TIPS

If I understand correctly your question is What happens when you do the following

DerivedClass derived = (DerivedClass)baseObj.SimpleClone();

Did you try that? Simply it will result in InvalidCastException since BaseClass is not DerivedClass.

I have answered a similar question here, That should clear things up.

The answer to your downcast is no, it can't be done as per SriRam's answer. Although rather messy, you could achieve your cloning by using polymorphicism. You will probably need to split out the concerns of creation and assignment when doing so:

public class BaseClass
{
    public int Value1 { get; set; }
    public int Value2 { get; set; }

    protected virtual BasedClass Create()
    {
       return new BaseClass();
    }

    public virtual BaseClass SimpleClone()
    {
        var clone = Create(); // The appropriate create will be called
        clone.Value1 = this.Value1;
        clone.Value2 = this.Value2;
        return clone;
    }
}

public class DerivedClass : BaseClass
{
    public bool Value3 { get; set; }

    public DerivedClass()
    {
        Value3 = true;
    }

    protected override BasedClass Create()
    {
       return new DerivedClass();
    }

    public override BaseClass SimpleClone()
    {
       var result = base.SimpleClone();
       (result as DerivedClass).Value3 = this.Value3;
    }
}

It doesn't work that way. Casting is way of telling the compiler to treat the object as if it was the object of casting class, and you are aware of information loss.

Just because a derived class is derived from base class doesn't necessarily mean that both are same.

So, you will get InvalidCastException.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top