a == b
一般的类
1 public class myRectangle
2 {
3 public double with;
4 public double length;
5 private double area;
6
7 public myRectangle(double with, double length)
8 {
9 this.with = with;
10 this.length = length;
11 area = with * length;
12 }
13 public double Area
14 {
15 get
16 {
17 return area;
18 }
19 set
20 {
21 area = value;
22 }
23 }
24
25 }
1 public class myRectangle
2 {
3 public double with;
4 public double length;
5 private double area;
6
7 public myRectangle(double with, double length)
8 {
9 this.with = with;
10 this.length = length;
11 area = with * length;
12 }
13 public double Area
14 {
15 get
16 {
17 return area;
18 }
19 set
20 {
21 area = value;
22 }
23 }
24
25 }
重写的类
1 public class Rectangle
2 {
3 public double with;
4 public double length;
5 private double area;
6
7 public Rectangle(double with,double length)
8 {
9 this.with = with;
10 this.length = length;
11 area = with * length;
12 }
13 public double Area
14 {
15 get
16 {
17 return area;
18 }
19 set
20 {
21 area = value;
22 }
23 }
24
25 public static bool operator == (Rectangle rectangle1,Rectangle rectangle2)//同时要求!=
26 {
27 if (rectangle1.with == rectangle2.with && rectangle1.length == rectangle2.length)
28 {
29 return true;
30 }
31 return false;
32 }
33
34 public static bool operator !=(Rectangle rectangle1, Rectangle rectangle2)
35 {
36 if (rectangle1.with != rectangle2.with && rectangle1.length != rectangle2.length)
37 {
38 return true;
39 }
40 return false;
41 }
42
43 public override int GetHashCode()
44 {
45 return area.GetHashCode();
46 }
47
48 public override bool Equals(object obj)
49 {
50 Rectangle r = (Rectangle)obj;
51 if (this.with == r.with && this.length == r.length)
52 {
53 return true;
54 }
55
56 return false;
57 }
58 }
1 public class Rectangle
2 {
3 public double with;
4 public double length;
5 private double area;
6
7 public Rectangle(double with,double length)
8 {
9 this.with = with;
10 this.length = length;
11 area = with * length;
12 }
13 public double Area
14 {
15 get
16 {
17 return area;
18 }
19 set
20 {
21 area = value;
22 }
23 }
24
25 public static bool operator == (Rectangle rectangle1,Rectangle rectangle2)//同时要求!=
26 {
27 if (rectangle1.with == rectangle2.with && rectangle1.length == rectangle2.length)
28 {
29 return true;
30 }
31 return false;
32 }
33
34 public static bool operator !=(Rectangle rectangle1, Rectangle rectangle2)
35 {
36 if (rectangle1.with != rectangle2.with && rectangle1.length != rectangle2.length)
37 {
38 return true;
39 }
40 return false;
41 }
42
43 public override int GetHashCode()
44 {
45 return area.GetHashCode();
46 }
47
48 public override bool Equals(object obj)
49 {
50 Rectangle r = (Rectangle)obj;
51 if (this.with == r.with && this.length == r.length)
52 {
53 return true;
54 }
55
56 return false;
57 }
58 }
客户代码
1 //a == b
2 /**//*
3 operator 关键字用于在类或结构声明中声明运算符。运算符声明可以采用下列四种形式之一:
4
5 public static result-type operator unary-operator ( op-type operand )
6 public static result-type operator binary-operator (
7 op-type operand,
8 op-type2 operand2
9 )
10 public static implicit operator conv-type-out ( conv-type-in operand )
11 public static explicit operator conv-type-out ( conv-type-in operand )
12
13 示例
14 以下是一个有理数的极其简化的类。该类重载 + 和 * 运算符以执行小数加法和乘法,同时提供将小数转换为双精度的运算符。
15
16 using System;
17 class Fraction
18 {
19 int num, den;
20 public Fraction(int num, int den)
21 {
22 this.num = num;
23 this.den = den;
24 }
25
26 // overload operator +
27 public static Fraction operator +(Fraction a, Fraction b)
28 {
29 return new Fraction(a.num * b.den + b.num * a.den,
30 a.den * b.den);
31 }
32
33 // overload operator *
34 public static Fraction operator *(Fraction a, Fraction b)
35 {
36 return new Fraction(a.num * b.num, a.den * b.den);
37 }
38
39 // define operator double
40 public static implicit operator double(Fraction f)
41 {
42 return (double)f.num / f.den;
43 }
44 }
45
46 补充
47
48 参数
49 result-type 运算符的结果类型。
50 unary-operator 下列运算符之一:+ - ! ~ ++ — true false
51 op-type 第一个(或唯一一个)参数的类型。
52 operand 第一个(或唯一一个)参数的名称。
53 binary-operator 其中一个:+ - * / % & | ^ << >> == != > < >= <=
54 op-type2 第二个参数的类型。
55 operand2 第二个参数的名称。
56 conv-type-out 类型转换运算符的目标类型。
57 conv-type-in 类型转换运算符的输入类型。
58
59 =号不可以重载
60 */
61
62 myRectangle myr = new myRectangle(12, 13);
63 myRectangle myr1 = new myRectangle(12, 13);
64 Assert.IsFalse(myr == myr1);
65
66 Rectangle myr2 = new Rectangle(12, 13);
67 Rectangle myr22 = new Rectangle(12, 13);
68 Assert.IsTrue(myr2 == myr22);
1 //a == b
2 /**//*
3 operator 关键字用于在类或结构声明中声明运算符。运算符声明可以采用下列四种形式之一:
4
5 public static result-type operator unary-operator ( op-type operand )
6 public static result-type operator binary-operator (
7 op-type operand,
8 op-type2 operand2
9 )
10 public static implicit operator conv-type-out ( conv-type-in operand )
11 public static explicit operator conv-type-out ( conv-type-in operand )
12
13 示例
14 以下是一个有理数的极其简化的类。该类重载 + 和 * 运算符以执行小数加法和乘法,同时提供将小数转换为双精度的运算符。
15
16 using System;
17 class Fraction
18 {
19 int num, den;
20 public Fraction(int num, int den)
21 {
22 this.num = num;
23 this.den = den;
24 }
25
26 // overload operator +
27 public static Fraction operator +(Fraction a, Fraction b)
28 {
29 return new Fraction(a.num * b.den + b.num * a.den,
30 a.den * b.den);
31 }
32
33 // overload operator *
34 public static Fraction operator *(Fraction a, Fraction b)
35 {
36 return new Fraction(a.num * b.num, a.den * b.den);
37 }
38
39 // define operator double
40 public static implicit operator double(Fraction f)
41 {
42 return (double)f.num / f.den;
43 }
44 }
45
46 补充
47
48 参数
49 result-type 运算符的结果类型。
50 unary-operator 下列运算符之一:+ - ! ~ ++ — true false
51 op-type 第一个(或唯一一个)参数的类型。
52 operand 第一个(或唯一一个)参数的名称。
53 binary-operator 其中一个:+ - * / % & | ^ << >> == != > < >= <=
54 op-type2 第二个参数的类型。
55 operand2 第二个参数的名称。
56 conv-type-out 类型转换运算符的目标类型。
57 conv-type-in 类型转换运算符的输入类型。
58
59 =号不可以重载
60 */
61
62 myRectangle myr = new myRectangle(12, 13);
63 myRectangle myr1 = new myRectangle(12, 13);
64 Assert.IsFalse(myr == myr1);
65
66 Rectangle myr2 = new Rectangle(12, 13);
67 Rectangle myr22 = new Rectangle(12, 13);
68 Assert.IsTrue(myr2 == myr22);
interest
1 // Create DataSet
2 DataSet ds = new DataSet("ds");
3 DataTable dt = ds.Tables.Add("dt");
4 dt.Columns.Add("Value", typeof(int));
5
6 // Add two rows, both with Value column set to 1
7 DataRow row1 = dt.NewRow(); row1["Value"] = 1; dt.Rows.Add(row1);
8 DataRow row2 = dt.NewRow(); row2["Value"] = 1; dt.Rows.Add(row2);
9 Assert.IsFalse(row1["Value"] == row2["Value"]);//The reason is that both row1[Value] and row2[Value] return objects, not integers
10 // Compare with == returns false.
11 Assert.IsTrue(row1["Value"].Equals(row2["Value"]));//This is because .Equals is overridden in System.Int32 to do a value comparison
12 // Compare with .Equals returns true.
1 // Create DataSet
2 DataSet ds = new DataSet("ds");
3 DataTable dt = ds.Tables.Add("dt");
4 dt.Columns.Add("Value", typeof(int));
5
6 // Add two rows, both with Value column set to 1
7 DataRow row1 = dt.NewRow(); row1["Value"] = 1; dt.Rows.Add(row1);
8 DataRow row2 = dt.NewRow(); row2["Value"] = 1; dt.Rows.Add(row2);
9 Assert.IsFalse(row1["Value"] == row2["Value"]);//The reason is that both row1[Value] and row2[Value] return objects, not integers
10 // Compare with == returns false.
11 Assert.IsTrue(row1["Value"].Equals(row2["Value"]));//This is because .Equals is overridden in System.Int32 to do a value comparison
12 // Compare with .Equals returns true.
String is especial
1object a = "Hello World";
2 object b = "Hello World";
3 Assert.IsTrue(a.Equals(b));
4 Assert.IsTrue(a == b);//because of an optimization in the CLR. The CLR keeps a list of all strings currently being used in an application in something called the intern pool. When a new string is set up in code the CLR checks the intern pool to see if the string is already in use. If so, it will not allocate memory to the string again, but will re-use the existing memory. Hence a == b is true above
5
6 object a1 = "Hello World";
7 object b1 = new StringBuilder().Append("Hello").Append(" World").ToString();
8 Assert.IsTrue(a1.Equals(b1));
9 Assert.IsFalse(a1 == b1);//a and b do not have the same adrress.
1object a = "Hello World";
2 object b = "Hello World";
3 Assert.IsTrue(a.Equals(b));
4 Assert.IsTrue(a == b);//because of an optimization in the CLR. The CLR keeps a list of all strings currently being used in an application in something called the intern pool. When a new string is set up in code the CLR checks the intern pool to see if the string is already in use. If so, it will not allocate memory to the string again, but will re-use the existing memory. Hence a == b is true above
5
6 object a1 = "Hello World";
7 object b1 = new StringBuilder().Append("Hello").Append(" World").ToString();
8 Assert.IsTrue(a1.Equals(b1));
9 Assert.IsFalse(a1 == b1);//a and b do not have the same adrress.