I would like to create an array/collection that contains objects. I would like to be able to access the objects in the array/collection with a key value. I’m thinking the syntax to access the objects would be something like this:
ObjectArray[52].Color = "Blue"
ObjectArray[52].Height = 12.2
If(ObjectArray[52].Color == "Blue")
{
// Code for Blue
}
If(ObjectArray[52].Height < 13.0)
{
// Code for height less than 13.
}
class ObjectInArray
{
public string Color;
public double Height;
}
“52” in this case is a key value, not a position in the array. Color and Blue are properties in the objects. I do not know how to create an array/collection to do this (if it can be done). Any help or optional suggestion how to do this would be appreciated.
You don’t want an array, you want something which can associate keys of one type with values of another type. Such collection in .Net is called
Dictionary<TKey, TValue>, specifically for your caseDictionary<int, ObjectInArray>.Example: