- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
有人能解释一下为什么我会得到不同的结果吗?
bool result = true;
for (int row = 0; row < State.Rows; row++)
{
for (int col = 0; col < State.Cols; col++)
{
if (map[row][col] != other.map[row][col])
//if (!map[row][col].Equals(other.map[row][col]))
result = false;
}
}
bool sequenceEqual = map.SequenceEqual(other.map);
//result = true
//sequenceEqual = false
I overwrote Equals()
, GetHashCode()
, ==
and !=
for the type to be compared .我还尝试了 this 中的 CollectionComparer
回答,得到相同的结果。
最佳答案
它们不等价。
SequenceEqual
对序列的每个元素使用 Equals
。由于 map
是一个二维数组,每个“元素”实际上是一行,这意味着它是 Array
的 Equals
方法结束的被召唤。
您的方法直接使用 !=
比较每个元素,这就是您想要的。
一种解决方案是为 SequenceEquals 提供自定义相等性比较器。然后,所述比较器将再次调用 SequenceEquals。
关于c# - 比较 Array[][] 与 SequenceEqual() 和自定义方法的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7934937/
在下面的代码中调用 SequenceEqual当 List 是用类泛型类型定义的(调用 EquatableClass.Equals<>)时,在泛型列表上返回 true(如预期的那样)。 如果列表定义为
我使用 c# 3.5 和 linq 编写了一个简单的程序。 我有课 public class Product { public Product() { } public
我在进行彩票模拟,我有 2 个不同的数组,其中包含 6 个数字,首先保存用户希望玩的数字,其次保存每次运行生成的数字。 用户将他们的数字输入文本框,并将其保存为字符串并放入数组中的相应位置,随机生成的
父类型: public class IdObject : IComparable, IEquatable { public int id { get; set; } public bo
我有两个 List我正在使用 SequenceEqual方法来确定它们是否匹配。 我现在需要一个 List, string, IEnumerable,失败的元素的任何索引。 有没有简单的方法? 谢谢
我有一个名为 Country 的类。它有公共(public)成员“CountryName”和“States”。 我已经声明了一份国家名单。 现在我想编写一个函数,它接受一个新的“国家”并确定 Coun
在 C# 中,是否有 IEqualityComparer使用 SequenceEqual判断是否相等的方法? 最佳答案 .NET Framework 中没有这样的比较器,但您可以创建一个: publi
任何人都可以帮助解决我遇到的一些问题。我正在尝试创建一种自制的身份验证方法,我有点卡在几个方面,希望有人能提供帮助。我首先想问的是如何解决我在代码中评论的问题: public string A
我正在编写一个简单的控制台应用程序来比较自定义类对象的两个实例。对于每个属性,我将 True 或 False 写入控制台窗口以显示每个对象的属性是否匹配。 某些属性,如 ProductLines(列表
我有以下 LINQ 语句(stationId 是一个 int 而 version 是一个字节数组): var foundStation = (from wd in _Conte
为什么Enumerable.SequenceEqual将其比较器视为 IEqualityComparer ?该算法似乎没有使用 GetHashCode .为什么不取一个 Func谓词,类似于 Firs
我一直在尝试实现 Set 相等性(即顺序不相关的列表比较),并在阅读了诸如 this 之类的问题之后和 this ,编写了以下简单扩展: public static bool SetEqual
我一直在尝试实现 Set 相等性(即顺序不相关的列表比较),并在阅读了诸如 this 之类的问题之后和 this ,编写了以下简单扩展: public static bool SetEqual
我很难弄清楚 SequenceEqual 在 Reactive Extensions 2.2.5 版中是如何工作的 下面是代码 var subject1 = new Subject();
有人能解释一下为什么我会得到不同的结果吗? bool result = true; for (int row = 0; row < State.Rows; row++) { for (int
我正在尝试使用以下逻辑检查字符串是否为回文的可能性 class Program { static void Main(string[] args) { Console.
我正在尝试使用 Enumerable.SequenceEqual(x,y) 因为我希望它基于Object.Equals(x,y) 方法,如果 x 或 y 为 null,则返回 false,如果两者均为
我正在尝试使用 Type.GetMethod(...) 获取 Enumerable.SequenceEqual 的 MethodInfo。到目前为止,我已经尝试了以下方法: var mi = type
来自 MSDN The SequenceEqual(IEnumerable, IEnumerable) method enumerates the two source sequences in pa
我有 2 个数组: int[] arr1 = new int[] { 1, 2, 3 }; int[] arr2 = new int[] { 1, 2, 3 }; 我需
我是一名优秀的程序员,十分优秀!