类“对象"中定义的==运算符在哪里? [英] Where is == operator defined in Class "object"?

查看:35
本文介绍了类“对象"中定义的==运算符在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了

编译器为以下程序生成的

(它是一个控制台应用程序):

  class程序{静态void Main(string [] args){int a = 3;int b = 4;bool areEqual = a == b;Console.WriteLine(areEqual);}} 

注意IL_0007行.发出了 ceq 指令.这就是您要寻找的 == 运算符.

重要提示

== 没有超载时,就会发生这种情况.

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?

解决方案

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.

这篇关于类“对象"中定义的==运算符在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆