Best way to store a list with different datatypes in C#?

Hi there, I want to have a list where I can store all tiles of my gamemap with additional info, such as the unit (gameobject) that is standing on that particular tile. Arrays don’t allow for different datatypes, is there any way except using an external database like SQL?

It should look something like this:

Tile 1 | Unit3 | Gras
Tile 2 | Empty | Gras
Tile 3 | Unit1 | Hills

Thanks in advance,

Thomas

If all your items (units, grass, hills) GameObjects, then you can use a Dictionary. Just whipped this together, probably not the best way to do it. (you might want to consider using a container/wrapper class to store a tile’s information)

Dictionary<TileClass, List<GameObject>> tileMap = new Dictionary<TileClass, List<GameObject>>();

//to register/add new tiles to the mapping:
tileMap.Add(Tile1, new List<GameObject>());
tileMap.Add(Tile2, new List<GameObject>());
tileMap.Add(Tile3, new List<GameObject>());

//to add new items to a tile
tileMap[Tile1].Add(Unit3);
tileMap[Tile1].Add(Grass);
tileMap[Tile2].Add(Grass);
tileMap[Tile3].Add(Unit1);
tileMap[Tile3].Add(Hills);

//to remove items from a tile
tileMap[Tile1].Remove(Unit3);

//to cycle through all tiles and all items on the tiles
foreach(KeyValuePair<TileClass, List<GameObject>> tileInfo in tileMap)
{
    TileClass currentTile = tileInfo.Key;
    List<GameObject> currentTileItems = tileInfo.Value;

    foreach(GameObject item in currentTileItems)
    {
        //"item" could be a unit, grass, tree, etc
        if (item is UnitClass)
        {
            //do something as a unit
        }
        else if (item is GrassClass)
        {
            //do something as grass
        }
        else if (item is HillsClass)
        {
            //do something as hills
        }
    }
}

EDIT: if all your items don’t inherit from GameObject, or some other common class, you can change all instances of “GameObject” above with “System.Object”.

You can have arrays of Struct or Classes.

like

public struct Tile
{
public int unit;
public int terrain;
}

or

public class Tile
{
public int unit;
public int terrain;
}

arrays would be like

public Tile[ ] tiles;
public Tile[,] tile2D;

Struct is better for small data sets, otherwise use class.

Thanks both of you. I will try this right after I solve another problem that is giving me headaches :).