1

I have this empty interface IProvider and another interface called ICountryProvider that inherits from IProvider. I'm storing different kinds of "providers" inside a dictionary which is declared like this.

Dictionary<string, IProvider> dic;

Now, when I want to get a specific "provider" implementation let it be ICountryProvider, I do the following:

var countryProvider = (ICountryProvider)dic["country"];

Now, I'm concerned about the performance because this will be executed on each request to my API. So, I want to ask, is this a casting operation and does it involve any reflection-related operation?


Also, I have side questions, does the runtime cache the result of such "conversion" if it's called a conversion? Is there any performance difference betwee "converting" from object to ICountryProvier and from IProvider to ICountryProvider?
Rickless
  • 1,377
  • 3
  • 17
  • 36
  • 3
    What makes you think this would use reflection? As an aside, this looks like a code smell to me. Any time you cast to a more specific interface tells me you're doing something wrong. – DavidG Sep 06 '18 at 09:27
  • 2
    Casting is not related to reflection and it's also not expensive to cast – Tim Schmelter Sep 06 '18 at 09:29
  • I don't know how the compiler implement this operation internally. How does the compiler figure out the new type from the old type without the need to read and process its meta-data. Also, if you please expand a little about the code smell you talked about it would be much appreciated, – Rickless Sep 06 '18 at 09:29
  • 2
    @Mahmoud: the compiler won't but the runtime and it will just do it. If it fails you will get an `InvalidCastException`. https://stackoverflow.com/a/2785082/284240 – Tim Schmelter Sep 06 '18 at 09:30
  • @TimSchmelter So this is a cast operation. I'm a little bit confused about casting, conversion, boxing/unboxing and which involves the use of reflection and which is not. – Rickless Sep 06 '18 at 09:32
  • @TimSchmelter Sorry my bad. Yes the run-time. So, no need for the type meta-data to execute such operation? – Rickless Sep 06 '18 at 09:33
  • 1
    @Mahmoud: boxing/unboxing is another topic which indeed _can be_ a performance bottleneck. If you have to type-cast value types often to object and back to the value type(say `int`), you should consider to use a different (generic) collection and don't use Object for everything. – Tim Schmelter Sep 06 '18 at 09:35
  • @TimSchmelter Thanks so much for the link. It helped me a lot. Should I report my question as duplicate? – Rickless Sep 06 '18 at 09:38

1 Answers1

1

The CLR (Command Language Runtime) manages the explicit casts at runtime, checking the objects type tree to make sure the cast is valid, i.e. the object being casted is compatible with the type. If this check fails it throws an InvalidCastException to preserve type safety.

There is no caching done as far as I'm aware.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • I was waiting for the caching answer, thanks! Now regarding the last side question, and since the CLR will have to check the object type tree, is there any difference between casting from top level type like object and direct higher level type like the `IProvider` in my example from the performance point. – Rickless Sep 06 '18 at 09:44