相关疑难解决方法(0)

当我使用is运算符时为什么在IL代码中只有空值检查?

我想知道如何is operator实现C#.我已经编写了一个简单的测试程序(没有什么特别的,仅用于演示目的):

class Base
{
    public void Display() {  Console.WriteLine("Base"); }
}

class Derived : Base { }

class Program
{
    static void Main(string[] args)
    {
        var d = new Derived();

        if (d is Base)
        {
            var b = (Base) d;
            d.Display();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并查看生成的IL代码:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       27 (0x1b)
  .maxstack  2
  .locals init ([0] class ConsoleApplication1.Derived d,
           [1] bool V_1,
           [2] …
Run Code Online (Sandbox Code Playgroud)

c# il

13
推荐指数
2
解决办法
690
查看次数

标签 统计

c# ×1

il ×1