我对 c# 还很陌生,我有一个问题。我已经用大量(数千个)自定义对象填充了 2 个 SortedSet,并且我正在尝试创建一个快速访问另一个 SortedSet 的函数,而无需对其进行迭代(迭代需要太多时间,大约 40 秒)。我已经按名称对集合进行了排序,然后使用接口进行了扩展:
public int CompareTo(card other)
{
if (string.Compare(this.name,other.name)>0)
return 1;
if (string.Compare(this.name, other.name) < 0)
return -1;
else if (string.Compare(this.expansion, other.expansion) > 0)
return 1;
else if (string.Compare(this.expansion, other.expansion) < 0)
return -1;
else
return 0;
} // in my obj declaration.
//and the following for declaring the sortedset:
public SortedSet<card> MM = new SortedSet<card>();
public SortedSet<card> MMother = new SortedSet<card>();
//i can do the following:
int counter=0;
foreach (card thing in MM) //took only .7 seconds
{
if(thing.IsProperSubsetOf(MMother))
counter++;
}
这很好用,但是,由于对象不仅有名称和扩展名,而且还有其他属性,例如 price= 10 、 owner = "jack" 等等等等。id 喜欢访问这些,无需迭代。就像是:
int price = (MMother.matches(card)-->return price)
但我不知道该怎么做。有小费吗?