Where is == operator defined in Class “object”?

前端 未结 4 1112
渐次进展
渐次进展 2021-02-05 00:46

I searched the source code of FCL, and I got confused that string.Equals() uses Object.ReferenceEquals(), and Object.ReferenceEquals() use

4条回答
  •  一向
    一向 (楼主)
    2021-02-05 01:12

    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:

    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.

提交回复
热议问题