Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# "is" operator - is that reflection?

A colleague asked me an interesting question today - is the C# keyword/operator "is" considered reflection?

object tmp = "a string";
if(tmp is String)
{
}

How is this operator implemented behind the scenes? Does it require reflection or introspection? Or because of the strongly typed nature of the language, is the Type of the object immediately accessable as a top-level attribute of the object in memory?

MSDN states that:

Note that the is operator only considers reference conversions, boxing conversions, and unboxing conversions. Other conversions, such as user-defined conversions, are not considered by the is operator.

The ability to consider boxed and unboxed conversions seems to imply to me some sort of introspection.

like image 633
Matt Avatar asked Jul 15 '09 20:07

Matt


People also ask

What C is used for?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".


2 Answers

Referencing ECMA-335, the is operator generates the isinst object model IL instruction (Partition III §4.6), which is part of the base instruction set as opposed to being part of the Reflection library (Partition IV §5.5).

Edit: The is operator is extremely efficient compared to the reflection library. You could perform basically the same test much more slowly via reflection:

typeof(T).IsAssignableFrom(obj.GetType())

Edit 2: You are not correct about the efficiency of the castclass and isinst instructions (which you've now edited out of the post). They are highly optimized in any practical VM implementation. The only real performance issue involved is the potential for castclass to throw an exception, which you avoid by using the C# as operator and a test for null (for reference types) or the is operator followed by a cast (for value types).

like image 99
Sam Harwell Avatar answered Oct 13 '22 13:10

Sam Harwell


The is operator essentially determines if a cast is possible, but instead of throwing an exception when the cast is impossible it returns false. If you consider casting reflection then this is also reflection.

EDIT:

After some research I have discovered that a cast is performed in IL på the castclass instruction while the is operator maps to the isinst instruction. FxCop has a rule that warns you if you are doing unecessary casts by first using the isinst and then the castclass instruction. Even though the operations are efficient they still have a performance cost.

like image 35
Martin Liversage Avatar answered Oct 13 '22 13:10

Martin Liversage