F
F
FlameArt2019-02-09 17:28:48
POP3
FlameArt, 2019-02-09 17:28:48

Why doesn't C# allow you to downcast from parent to child?

I wonder why C# fundamentally does not allow you to extend the base instance, as in this code?

class People { public string name; }
class Person : People { public uint id; }

static void Main(string[] args)
{

        People jack = new People { name = "Jack" };
        Person jack_passport = (Person)jack; // InvalidCastException

}

I want to understand on a theoretical level.
The task is simple: my library returns an instance, I want to supplement it and use it in my other application. It sounds nice and architecturally logical to extend this instance in the application and add your data there. But why is this not possible?
There are only some strange options left, such as copying all the properties manually, or creating a variable with a reference to the parent object. Or, even stranger, initialize an instance of a known type immediately in the library, do an Upcast to the base class, and then cast it to your own through Downcast, and such a cast works. In theory, this way should not exist in nature, because the library should not know about any derived classes.
I also wonder what the right approaches or architectures are for what I want.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
F
freeExec, 2019-02-09
@freeExec

I wonder why C# fundamentally does not allow you to extend the base instance, as in this code?

Because the object is created and occupies 20 bytes in memory. And oh no, it can’t be cast into an object that should occupy 20 MB.
Therefore, whatever one may say, you will have to create a new instance.

T
tex0, 2019-02-09
@tex0

I want to understand on a theoretical level.

You have two sets:
1) Person - the larger set, the derivative.
2) People - the parent, is a subset of Person and enters it completely.
So - from a larger (derived) set, you can select a subset, because this subset is wholly included in the derivative. But it is impossible to single out a larger (derivative) from a smaller (parent) one. You stupidly do not have data about the remaining elements of the derived set.
And now on the fingers:
You have a piece of paper. It is a composite object of all potential (!!!) arbitrary parts of it (cut out areas, torn off pieces, etc.). It turns out that physically you can select / tear off / cut out any piece that is in its composition from this piece of paper (reducing the larger to the smaller), but from any smaller piece of this sheet you can’t get a general, derivative one.
That is why the reduction of the base to the derivative is an incorrect operation from the point of view of logic and is, in your case, a mistake.

D
Decadal, 2019-02-09
@Decadal

Why does it seem logical to you to cast a parent instance into a child instance? And if the descendant defines the special conditions that are needed for its creation? What about dependencies?

R
Roman, 2019-02-10
@yarosroman

This is not fundamentally prohibited by C#, but by OOP.

A
Alexey, 2020-01-17
@Alexeee

It is strange that the following was not written here:

class People { public string name; }
class Person : People { public uint id; }

static void Main(string[] args)
{

        People jack = new Person { name = "Jack" }; // Upcast
        Person jack_passport = (Person)jack;  // Downcast

}

To be able to do a Downcast, you must first do an Upcast.
In theory, such code should work, but I'm not sure if the author needed it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question