小编Edd*_*uez的帖子

参考类型的Equals()的"最佳"规范实现是什么?

为引用类型实现Equals()比看起来更难.我目前的规范实现如下:

public bool Equals( MyClass obj )
{
  // If both refer to the same reference they are equal.
  if( ReferenceEquals( obj, this ) )
    return true;

  // If the other object is null they are not equal because in C# this cannot be null.
  if( ReferenceEquals( obj, null ) )
   return false;

   // Compare data to evaluate equality    
   return _data.Equals( obj._data );
}

public override bool Equals( object obj )
{
  // If both refer to the same reference …
Run Code Online (Sandbox Code Playgroud)

.net c# equals

6
推荐指数
1
解决办法
1149
查看次数

标签 统计

.net ×1

c# ×1

equals ×1