我想创建一个包含对象的数组/集合。我希望能够使用键值访问数组/集合中的对象。我认为访问对象的语法应该是这样的:
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”在这种情况下是一个键值,而不是数组中的位置。 Color 和 Blue 是对象中的属性。我不知道如何创建数组/集合来执行此操作(如果可以的话)。任何帮助或可选建议如何做到这一点将不胜感激。
请您参考如下方法:
我认为字典就是你要找的东西
Dictionary<int, ObjectInArray > dict= new Dictionary<int, ObjectInArray >();
