I have a class implementing an interface as follows:
public class Database : IStore
In another class I have the following member variable; and an instance of Database is dynamically assigned to it at run-time:
private IStore store;
and a method that does a type check on store variable as follows:
if (store is Database)
ReSharper claims the condition will always be false. Also, it claims “store as Database” will always be null. Why is that? What is the best way to check the run-time object type held by this interface-type member? Is it possibly just saying this ‘may not be true’ always or really that it ‘never will be’?
It sounds like either R# is confused, or you’ve got two different
IStoretypes, and you’re testing against one when actually the class implements a different one. (Or two differentDatabasetypes, of course. The possibilities are equivalent.)Are you able to produce a short but complete example which demonstrates the problem? If you hover over
IStoreandDatabasein the code with theas/isoperators, does it show the fully-qualified type names you’d expect? If you run the code, does it ever go into theifbody?