简体   繁体   English

当GetType()。Name匹配时,DataGridViewCell类型不匹配?

[英]DataGridViewCell Type mismatch, when GetType().Name matches?

When iterating over a collection of DataGridViewCell s in a DataGridViewRow , I was testing for equality of a specific cell like so: 当遍历DataGridViewCell中的DataGridViewRow的集合时,我正在测试特定单元格的相等性,如下所示:

var transactionLogId = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex].Cells[0].Value;
if (dgvRow.Cells[0].Value == transactionLogId) {
    // Snip
}

And to my dismay, it never entered the nested block. 令我沮丧的是,它从未进入嵌套块。 Though both equal the same ( 5177518 ), and both dgvRow.Cells[0].Value.GetType().Name and transactionLogId.GetType().Name are Int64 , it doesn't work unless I append a .ToString() to both sides of the equality check. 尽管两者都相同( 5177518 ),并且dgvRow.Cells[0].Value.GetType().NametransactionLogId.GetType().Name都为Int64 ,但除非将.ToString()附加到它,否则它将不起作用双方均等检查。

Curious as to what I'm missing here. 对我在这里想念的东西感到好奇。 Thanks! 谢谢!

Operators are static calls, which means that overload resolution will resolve based strictly on the operand's compile time types. 运算符是静态调用,这意味着重载解析将严格根据操作数的编译时间类型进行解析。

In your case, overload resolution is resolving to the following operator overload: 在您的情况下,重载解决方案可解决以下运算符重载:

==(object, object)

because those are the compile time types of the operands. 因为这些是操作数的编译时类型。 The == for objects simply performs a reference equality check, that is, if both objects are the same object, which they clearly are not. 对象的==只是执行引用相等性检查,也就是说,如果两个对象都是同一个对象,则显然不是。

The same happens in the following code: 在以下代码中也会发生相同的情况:

object o1 = 1;
object o2 = 1;
var equal = o1 == o2; //false

Using Equals in this case is the correct thing to do, because it is a virtual call and it will therefore resolve to the runtime type of the callee and give you the correct result. 在这种情况下,使用Equals是正确的做法,因为它是一个虚拟调用,因此它将解析为被调用方的运行时类型,并为您提供正确的结果。

Then why does this work when you call ToString() on both operands? 那么当您在两个操作数上都调用ToString()时,为什么此方法起作用? Simply because the overload resolution resolves to the ==(string, string) overload implented in the string class which performs value equality. 仅仅因为重载解析可以解决==(string, string)重载在执行值相等的string类中。

UPDATE UPDATE

Reading comments by Machine Learning there seems to be a misunderstanding concerning the reason why == is returning false in the OP's code. 通过Machine Learning阅读注释似乎对OP中的==返回false的原因存在误解。 I want to clarify that boxing has nothing to do with it; 我想澄清一下,拳击与它无关。 consider the following example: 考虑以下示例:

var str1 = "a";
var str2 = 'a'.ToString(); //to avoid string interning by the compiler.
var equals = str1 == str2; //true
object o1 = str1;
object o2 = str2;
equals = o1 == o2; //false

The observed behavior is exactly the same and there is no boxing/unboxing happening here. 观察到的行为是完全相同的,并且此处没有装箱/拆箱。

They are both boxed values so the == operator will never be true. 它们都是盒装值,因此==运算符永远不会为true。

GetType returns the type of the corresponding unboxed instances anyway. 无论如何,GetType返回相应的未装箱实例的类型。

You need to use Equals. 您需要使用等于。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM