我有以下代码来查找 List<ColorItem>
中 ColorItem
对象的索引:
//Get the index of the color item
var colorList = dialogViewModel.Items;
var colorItem = new ColorItem();
colorItem = sp.TileColorItem;
int index = colorList.IndexOf(colorItem);
尽管列表中有匹配的对象,但
index
始终返回-1。我错过了什么吗?
ColorItem
是你自己的类吗?如果是,你可以通过重写Equals
方法来使其工作...但我们需要上下文。 - Jon Skeet