开发者

Overloading == operator

A derives directly from Object class and neither A or Object overload == operator, so why doesn't next code cause an error:

class Program
{
    sta开发者_Python百科tic void Main(string[] args)
    {
        A a1 = new A();
        A a2 = new A();

        if (a1 == a2) ... ;
    }
}

class A { }

thanx


A derives directly from Object class and neither A or Object overload == operator, so why doesn't next code cause an error?

As with your other question, you seem to have some strange belief that whether an overloaded operator exists has any bearing on whether an operator can be meaningfully chosen. It does not.

Again, to resolve this situation overload resolution first attempts to determine if there is a user-defined operator defined on either of the operands. As you note, there is not.

Overload resolution then falls back on the built-in operators. As I mentioned in your other question, the built-in operators are the equality operators on int, uint, long, ulong, bool, char, float, double, decimal, object, string, all delegate types and all enum types, plus the lifted-to-nullable versions of all the value types.

Given those operators we must now determine the applicable ones. There is no implicit conversion from "A" to any of the value types, to any of the nullable value types, to string, or to any delegate type.

The only remaining applicable candidate is object.

If overload resolution chooses the equality operator that compares two objects, additional constraints must be met. In particular, both operands must either be null or a reference type, or a type parameter not constrained to be a value type. That constraint is met. Also, if the two sides have types then the operand types must have some sort of compatibility relationship; you can't do "myString == myException" because there is no relationship between string and Exception. There is a relationship between "A" and "A", namely, they are identical.

Therefore the reference equality operator is chosen, and the == means "compare these two object expressions by reference".

I am mystified as to why you believe having a user-defined == operator has anything to do with this, either in this question or your other question. The absence of such a method does not prevent the compiler from generating whatever code it likes for this expression. Can you explain?


Because by default, the == operator compares the references (memory locations) of the objects a1 and a2. And because they're different instances of class A, the expression a1 == a2 always evaluates to false in your example.


Objects have a default implementation of the == operator that checks if they refer to the same object (reference comparison). So there's no reason for it to be an error. The operator does have a meaning.


Because Object has a default implementation comparing references.


The base's == operator is called that why its not giving any error.


By default, the operator == tests for reference equality by determining if two references indicate the same object, so reference types do not need to implement operator == in order to gain this functionality.

From: http://msdn.microsoft.com/en-us/library/ms173147(v=vs.80).aspx

The important bit concerning your question being:

reference types do not need to implement operator == in order to gain this functionality

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜