31

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.

Matt
  • 41,216
  • 30
  • 109
  • 147

3 Answers3

41

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).

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • I haven't edited anything out, but perhaps I should clarify what I mean by "expensive". I'm don't know much about IL efficiency, but FxCop has a warning if your create code that emits both an isinst and castclass instruction as they consider it ineffecient or "expensive". – Martin Liversage Jul 15 '09 at 21:25
  • 2
    As I mentioned in the second edit, you should use `as` followed by a `null` check for reference types. You instead used `is` followed by a cast, which is what FxCop is alerting you to. – Sam Harwell Jul 15 '09 at 21:35
  • 2
    For what it's worth, on the Compact Framework, IL that needs to explicitly check RTTI (such as `isinst` and `castclass`) is as slow as reflection, while callvirt is highly optimized. The moral: try to avoid using type-case constructs where virtual/abstract methods can work. – Jeffrey Hantin Oct 19 '09 at 21:18
  • 1
    @SamHarwell that link to ECMA-335 is broke, do you know an updated version? – shanabus Oct 11 '18 at 13:07
5

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.

Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
1

Other languages have runtime time information sufficient to support dynamic casting, and yet nothing that could be described as reflection (C++ being an obvious example).

So reflection refers to additional capabilities beyond merely discovering the type of an object. To "reflect" on an object implies the ability to walk its members, for example.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
  • It would be *more* correct to say that reflection is parsing the members of a class than those of an object, since you do not need to have an instance alive. Also reflection enables "discovering" what classes you have in an assembly, not only members and other info of a class. – Danny Varod Aug 29 '11 at 14:43
  • @Danny Varod - Not all languages have classes, or assemblies. – Daniel Earwicker Aug 29 '11 at 21:21
  • C++ has classes and libraries, Java has classes and packages. Object Oriented Programming does define the difference between an object and a class. – Danny Varod Aug 30 '11 at 09:42
  • Also, C++ could not have reflection - since the binary files do not have the required info embedded in them. - That is why you need h files to consume a binary and lib files to link, expect from the assemblies to run. – Danny Varod Aug 30 '11 at 09:45
  • 1
    @Danny Varod - Languages with prototypal inheritance (Self, JavaScript) do not have classes, but they may have reflection (JavaScript allows you to walk through the members of an object). – Daniel Earwicker Sep 01 '11 at 13:47