Saving a heterogeneous array in C #

I have List<Animal>, but there are subclasses in this list Animal, such as Mammal.

I have to support it using the [Serializable] interface, and this is not a problem, except when I read it again, I have no idea which subclass the data was received from when it came time to restore the information.

So the question is how to store this information?

+3
source share
2 answers

You do not need to take care of this yourself, as the structure serializes this type, and also knows which instance will be created before the content is deserialized. Just create a sample and you will see what happens ...; -)

+4
source

and how keywords will be friends for you.

Good to know that keyword tries to translate and returns true or false depending on whether the translation was successful.

If you want an actual object and are not just interested in its type, you better use the keyword how , and then check it for null . This saves you from having to repeat it once you have identified the type.

+1
source

Source: https://habr.com/ru/post/1751804/


All Articles