Get class from an array of a base class with derived classes

Hi!
So I have made an inventory system that stores items in a base class array (Item)
which can have more classes that derive from it (Sword, Armor…).
I tried (for testing) doing “Sword test = items[0] as Sword;” which works, but how do I know what class is in that array, since in this test I already knew that item 0 is a sword?

If you want to get a string representation, you can call items[0].GetType().ToString(), which will return you the full type as a string, but that’s of little value for more than debugging. Usually, you’ll just have a large if block, something like the following.

if(items[0] is Sword) {
    Sword test = items[0] as Sword;
}

Or, if you actually want the type to display, it would probably be best to add a field to your base class, something like Description, that you can just hard code in each derived class.