What is the cost of “how” compared to QueryInterface in COM or dynamic_cast in C ++?

I'm still trying to match my deep and old knowledge with C / C ++ with my more superficial .Net knowledge. Today the time has come "how" (and implicitly "is" and cast) in C #.

My mental “how” model is that it is a QueryInterface or dynamic_cast (a dynamic_cast with a pointer argument, not a link, that is) for C #. My question is twofold:

  • Is my comparison fair?
  • What is the relative cost of "how" compared to QueryInterface or dynamic_cast ?
+6
c ++ casting c # type-inference
source share
1 answer
  • Yes, the comparison is fair, especially when working with pointers. Each of the three either succeeds, or returns a non-zero pointer to the target type, or returns null.

  • In fact, you can use the as operator when working with COM objects in .NET, which makes it equivalent to QueryInterface with a small amount of overhead for managed / COM interaction. Inside the CLR (casting between managed types), the as operator is extremely lightweight compared to QueryInterface in COM or dynamic_cast in C ++. For all the places in my code where for some reason I had to use dynamic casting, I never saw the as operator show even one pattern in profiling - and given that I support the implementation of dynamically typed, time-bound language (StringTemplate ), I guess this means something. :)

+4
source share

All Articles