在.Contains对工作.Equals方法.默认情况下,如果两个实例(引用)相同,则该.Equals方法仅返回true.
解决这个问题的一种可能方法 - 如果因素的数量是固定的 - 使用a Tuple<int,int>.您可以Reverse在`Tuple类上定义方法:
public static class Foo {
public static Tuple<T2,T1> Reverse<T1,T2> (this Tuple<T1,T2> tuple) {
return new Tuple<T2,T1>(tuple.Item2,tuple.Item1);
}
}
Run Code Online (Sandbox Code Playgroud)
然后简单地用以下方法调用它:
Tuple<int,int> t = new Tuple<int,int>(3,5);
Tuple<int,int> t2 = t.Reverse();
Run Code Online (Sandbox Code Playgroud)
如果没有,您可以定义一个包装类,它执行此处描述的相等性检查.
或者另一种.Contains方法是在@xanatos answer所描述的方法中自己提供相等检查器.
演示:
$ csharp
Mono C# Shell, type "help;" for help
Enter statements below.
csharp> var t1 = new Tuple<int,int>(3,2);
csharp> var t2 = new Tuple<int,int>(3,2);
csharp> t1.Equals(t2);
true
csharp> int[] t1 = new int[] {3,2};
csharp> int[] t2 = new int[] {3,2};
csharp> t1.Equals(t2);
false
Run Code Online (Sandbox Code Playgroud)