Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

c# - Where is == operator defined in Class "object"?

I searched the source code of FCL, and I got confused that string.Equals() uses Object.ReferenceEquals(), and Object.ReferenceEquals() uses == operator to jugde. And then I can't find how the == operator is defined.

So where is the original operator defined?

question from:https://stackoverflow.com/questions/34172634/where-is-operator-defined-in-class-object

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is an operator that the language uses to validate that two values are the same. When your code would be compiled this operator would be compiled appropriately in CIL and then when we will be executed by the CLR the two values would be compared to be checked if they are the same.

For instance, this is the CIL code for the Main method:

Enter image description here

that the compiler produces for the following program (it's a console application):

class Program
{
    static void Main(string[] args)
    {
        int a = 3;
        int b = 4;
        bool areEqual = a == b;
        Console.WriteLine(areEqual);
    }
}

Note the IL_0007 line. There a ceq instruction has been emitted. This is that you are looking for, the == operator.

Important Note

This is happening when the == is not overloaded.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...