is the "is" operator just syntactic sugar for the "IsInstanceOfType" method

rud*_*ter 5 c# types keyword isinstance

Are the following code snippets equivalent?

class a
{}

class b:a
{}

b foo=new b();
Run Code Online (Sandbox Code Playgroud)

//here it comes

foo is a
Run Code Online (Sandbox Code Playgroud)

//...is the same as...

typeof(a).isinstanceoftype(foo)
Run Code Online (Sandbox Code Playgroud)

或者也许其中一个其他类型方法映射更接近于运算符.例如"IsAssignableFrom"或"IsSubclassOf"

Ond*_*cny 6

它不是,因为is在左侧容忍空引用.


Fre*_*örk 3

不,这不对。事实上,如果您仔细观察,IsInstanceOfType您会发现第一个代码行实际上使用 执行比较is,这将有效地导致StackOverflowExceptionif 是这种情况。

is运算符导致isinstIL 代码中的操作。