我定义了
public static bool operator ==(MyShape lhs, object rhs).....
public static bool operator !=(MyShape lhs, object rhs).....编译没有问题,怎么一和NULL比较就报错?
MyShape shape1 = null;
...
if (shape1 == null)...
public static bool operator ==(MyShape lhs, object rhs).....
public static bool operator !=(MyShape lhs, object rhs).....编译没有问题,怎么一和NULL比较就报错?
MyShape shape1 = null;
...
if (shape1 == null)...
解决方案 »
- list.FindAll(delegate(string s){return s.Contains(Str);}); Str 是个"沈阳,葫芦岛,建昌,绥中,
- c/s服务端与客务端通信问题
- 这这这这怎么实现?
- PreFilterMessage的重写问题
- 在线等Form2的实例中,怎么引用Form1的实例变量.
- 如何自定义LineCap 就是直线的线帽
- 请问在使用C#.net编写的TcpIp通讯程序中,Server端如何检测到Client端已把网线拔了?
- tcp怎么发请求
- 问一个数据类型转换的小问题。。。。。。。
- CrystallReport的问题(急啊)关于动态添加控件的问题!
- WPF 窗体传值的问题
- datetimePicker日历控件状态能在中-英文间切换吗?
{
if( object.ReferenceEqual(lhs, rhs) ) return true;
if( object.ReferenceEqual(lhs,null) || object.ReferenceEqual(rhs,null) ) return false;
...
}
public static bool operator !=(MyShape lhs, object rhs)
{
return !(lhs == rhs);
}
1. 先重写Equals
2. 再重写GetHashCode
3. 再重写 == 和 !=
是推荐的做法,否则会有某些缺陷产生
以下是我写的,不一定非常完善,供参考,可以解决 == null 的问题 public override bool Equals(object obj)
{
//Check for null
if (obj == null)
return false;
if (obj.GetType() != typeof(BasicQuoteInfo))
{
return false;
}
return this.Equals(obj as BasicQuoteInfo);
//这个Equals 是另外重新写的,真正的同类比较
} /// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="q1">The q1.</param>
/// <param name="q2">The q2.</param>
/// <returns>
/// The result of the operator.
/// </returns>
public static bool operator ==(BasicQuoteInfo q1, BasicQuoteInfo q2)
{
if (((object)q1 == null && (object)q2 != null) || ((object)q1 != null && (object)q2 == null))
return false;
if ((object)q1 == null && (object)q2 == null)
return true;
return q1.Equals(q2);
}
谢谢!!!!按你的方法解决了!!
是
object.ReferenceEquals
不是
object.ReferenceEqual谢谢!!!
分全给你