Nó là một operator overload
(của ==, không phương pháp quá tải của ReferenceEquals
) để kiểm tra xem hai trường hợp của loại Shop
là tài liệu tham khảo tương đương (có nghĩa là, cho dù họ đề cập đến cùng địa chỉ bộ nhớ).
bool result = shop1 == shop2; //shop1 and shop2 are of type Shop
Khi tuyên bố ==
điều hành, bạn cũng sẽ được yêu cầu phải quá tải phù hợp của nó (hoặc truy cập) điều hành !=
:
public static bool operator ==(Shop lhs, Shop rhs) {
if (Object.ReferenceEquals(lhs, null)) { //Check if the left-hand-side Shop is null
if (Object.ReferenceEquals(rhs, null)) {
return true; //both are null, equal reference
}
return false; //lhs is null, but rhs is not (not equal reference)
}
return lhs.Equals(rhs); //lhs is not null, thus can call .Equals, check if it is Equals to rhs
}
public static bool operator !=(Shop lhs, Shop rhs) { //the opposite operator
if (Object.ReferenceEquals(lhs, null)) {
if (Object.ReferenceEquals(rhs, null)) {
return false;
}
return true;
}
return !lhs.Equals(rhs);
}
Nó cũng đáng để lưu ý rằng Object.ReferenceEquals(lhs, null)
được sử dụng thay vì lhs == null
như thứ hai sẽ dẫn đến tình trạng quá tải khác ==
được gọi đến infinite recursion gây ra StackOverflowException
.
Chúng được sử dụng như thế này:
Shop shop1 = new Shop();
Shop shop2 = new Shop();
bool result = shop1 == shop2; //this will return false, since lhs and rhs referring to two different memory address
shop2 = shop1;
result = shop1 == shop2; //this will return true, referring to the same memory location
shop1 = null;
shop2 = null;
result = shop1 == shop2; //this will return true, both are null
Hiểu được điều này, bạn thậm chí có thể tạo ra một cái gì đó như thế này:
public struct MyCrazyInt{ //this will reverse the result of + and -
private int Value { get; set; }
public MyCrazyInt(int value) :this() {
Value = value;
}
public bool Equals(MyCrazyInt otherCrazy) {
return this.Value != otherCrazy.Value; //reverse this result
}
public static MyCrazyInt operator +(MyCrazyInt lhs, MyCrazyInt rhs) {
int lhsVal = lhs.Value;
int rhsVal = rhs.Value;
return new MyCrazyInt(lhsVal - rhsVal); //note that direct lhs-rhs will cause StackOverflow
}
public static MyCrazyInt operator -(MyCrazyInt lhs, MyCrazyInt rhs) {
int lhsVal = lhs.Value;
int rhsVal = rhs.Value;
return new MyCrazyInt(lhsVal + rhsVal); //note that direct lhs+rhs will cause StackOverflow
}
public override string ToString() {
return Value.ToString();
}
}
Và sau đó sử dụng nó như
MyCrazyInt crazyInt1 = new MyCrazyInt(5);
MyCrazyInt crazyInt2 = new MyCrazyInt(3);
MyCrazyInt crazyInt3 = crazyInt1 - crazyInt2; //this will return 8
crazyInt3 = crazyInt1 + crazyInt2; //this will return 2
'Object này. ReferenceEquals' kiểm tra nếu các tham chiếu bằng nhau ...;) - Nói cách khác, nó sẽ kiểm tra nếu đối tượng là đối tượng chính xác * *, về mặt vật lý m địa chỉ emory. – Rob
Có thể trùng lặp của [C# .Equals(), .ReferenceEquals() và == toán tử] (http://stackoverflow.com/questions/3869601/c-sharp-equals-referenceequals-and-operator) – Rohit
Kể từ khi == toán tử của lớp Shop bị quá tải, mã sẽ tránh sử dụng nó để kiểm tra tham số cho tham chiếu null. nếu (lhs == null) sẽ gây ra một đệ quy vô hạn và ứng dụng sẽ chỉ đơn giản là sụp đổ với một ngoại lệ tràn ngăn xếp. –