C# 没有从';发电机室';至';System.i可比较
好吧,我遵循了很多教程,但我从未找到解决方案 我在静态类中有此函数:C# 没有从';发电机室';至';System.i可比较,c#,templates,compareto,icomparable,C#,Templates,Compareto,Icomparable,好吧,我遵循了很多教程,但我从未找到解决方案 我在静态类中有此函数: public static bool isDifferent<T>(List<T> list1, List<T> list2) where T : IComparable { foreach (T item1 in list1) { bool different = false; fore
public static bool isDifferent<T>(List<T> list1, List<T> list2) where T : IComparable
{
foreach (T item1 in list1)
{
bool different = false;
foreach (T item2 in list2)
{
if (item2.CompareTo(item1) != 0)
different = true;
else
{
different = false;
break;//fuck yes i will use a break
}
}
if (different)
return true;
}
return false;
}
所以,每个房间都有一个走廊id列表,这是我需要比较的唯一值
我遵循了许多教程,它们的结构似乎与我的相同。从您的问题中,我可以推断您实现了接口IComparable 但是,此接口IComparable不可分配给IComparable
实现IComparable,它声明了方法“public int CompareTo(objectother)”。您的问题是
Room
实现了IComparable
,它与IComparable
的接口不同。您可能应该将方法更新为:
public static bool isDifferent<T>(List<T> list1, List<T> list2) where T : IComparable<T>
publicstaticbool是不同的(列表1,列表2),其中T:IComparable
听起来像是一个很糟糕的问题。我知道,我不明白为什么我在网上找到的例子与我的基本相同this@Jonesy当我的dungGenerator中有空间时,我吃东西。你能发布引发异常的代码吗?if(Methods.isDifferent(rooms,hallways[I].rooms))公共教室:IComparable{public List doors;public int CompareTo(Room other){if(other==null)返回1;if(Methods.isDifferent(doors,other.doors))返回1;else返回0;}@CédricRaymond,正如我所说的,我已经推断出了这一点(本在他的回答中也是如此……)很抱歉,我没有注意到你的答案也很好,但他的答案更清晰=pi也不得不出于同样的原因编辑我的问题。我不明白为什么有人否决了我的问题。这是一个合法的问题我也不知道否决票。混淆IComparer和IComparer是很常见的(即使是在代码审查或同行讨论中,你只会说“this type implements IComparer”之类的话,然后每个人都开始问“哪一个,泛型还是非泛型?”))谢谢!这很有效,所以我只遗漏了一小部分。
public static bool isDifferent<T>(List<T> list1, List<T> list2) where T : IComparable<T>