Gre*_*egC 7 .net c# collections pointers reference
在编写为遵循声明式样式的.NET程序中,ReferenceEquals()的一些合法用途是什么?
Chr*_*ain 12
不确定"写入遵循声明式样式" ReferenceEquals是什么意思,但通常在覆盖==运算符时使用.来自http://msdn.microsoft.com/en-us/library/ms173147.aspx:
public static bool operator ==(ThreeDPoint a, ThreeDPoint b)
{
// If both are null, or both are same instance, return true.
if (System.Object.ReferenceEquals(a, b))
{
return true;
}
// If one is null, but not both, return false.
if (((object)a == null) || ((object)b == null))
{
return false;
}
// Return true if the fields match:
return a.x == b.x && a.y == b.y && a.z == b.z;
}
Run Code Online (Sandbox Code Playgroud)
请务必参阅下面的注释:
注意: operator ==的重载中的常见错误是使用(a == b),(a == null)或(b == null)来检查引用相等性.这反而创建了对重载运算符==的调用,从而导致无限循环.使用ReferenceEquals或将类型转换为Object,以避免循环.
Eri*_*ert 11
在编写为遵循声明式样式的.NET程序中,ReferenceEquals()的一些合法用途是什么?
无论编写程序的"样式"如何,只有一个合法使用的ReferenceEquals:比较两个引用以获得引用相等性.
如果您使用ReferenceEquals而不是比较两个引用以获得引用相等性,那么您可能做错了什么.
| 归档时间: |
|
| 查看次数: |
876 次 |
| 最近记录: |