首页新闻找找看学习计划

引用类型操作符重载问题

0
悬赏园豆:10 [已解决问题] 解决于 2014-03-03 08:48

需要对一个类进行相等(==)与不相等(!=)的 操作符重载,结果进入了死胡同。

反射OBJECT的EQUALS,发现都是直接调用==来实现的。

参考代码:

 

 1     public class MyClass
 2     {
 3         public MyClass(int value)
 4         {
 5             this.Value = value;
 6         }
 7 
 8         public int Value
 9         {
10             get;
11             private set;
12         }
13 
14         public sealed override bool Equals(object obj)
15         {
16             return Equals(obj as MyClass);
17         }
18 
19         public bool Equals(MyClass obj)
20         {
21             if(obj == null)
22             {
23                 return false;
24             }
25             return this.Value == obj.Value;
26         }
27 
28         public override int GetHashCode()
29         {
30             return base.GetHashCode();
31         }
32 
33         public static bool operator ==(MyClass left, MyClass right)
34         { 
35             if(left == null && right == null)
36             {
37                 return true;
38             }
39             if(left == null)
40             {
41                 return right.Value == 0;
42             }
43             if(right == null)
44             {
45                 return left.Value == 0;
46             }
47             return right.Value == left.Value;
48         }
49 
50         public static bool operator !=(MyClass left, MyClass right)
51         {
52             return !(left == right);
53         }
54 
55         public static implicit operator int(MyClass data)
56         {
57             if (data == null)
58             {
59                 return 0;
60             }
61             return data.Value;
62         }
63 
64         public static implicit operator MyClass(int data)
65         {
66             return new MyClass(data);
67         }
68 
69         public static bool operator ==(MyClass left, int right)
70         {
71             if (left == null)
72             {
73                 return right == 0;
74             }
75             return left.Value == right;
76         }
77 
78         public static bool operator !=(MyClass left, int right)
79         {
80             return !(left == right);
81         }
82 
83         public static bool operator ==(int left, MyClass right)
84         {
85             return right == left;
86         }
87 
88         public static bool operator !=(int left, MyClass right)
89         {
90             return !(right == left);
91         }
92     }
519740105的主页 519740105 | 大侠五级 | 园豆:5810
提问于:2014-02-28 16:33
< >
分享
最佳答案
0

重载 == 时候不能使用 (left == null) 或 left == right  之类的代码的,它们会再次调用 == 函数,从而成为无限循环。正确的版本是这样的。

 1     public static bool operator ==(MyClass left, MyClass right)
 2     {
 3         if (object.ReferenceEquals(left, right))
 4         {
 5             return true;
 6         }
 7 
 8         if ((object)left == null || (object)right == null)
 9         {
10             return false;
11         }
12 
13         return right.Value == left.Value;
14     }
15 
16     public static bool operator !=(MyClass left, MyClass right)
17     {
18         return !(left == right);
19     }
收获园豆:10
天方 | 大侠五级 |园豆:5264 | 2014-02-28 23:35

谢谢!

那下面的代码(反射自系统)是否会调用自己写的这个==操作符呢?

public static bool ReferenceEquals(object objA, object objB)
{
    return (objA == objB);
}
519740105 | 园豆:5810 (大侠五级) | 2014-03-01 12:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册