我想知道如何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)