After refactoring some code recently, which involved some class renames, some of my code broke in a surprising way. The cause was a failing "is" operator test, that I was very surprised wasn't a compiler error or warning.
This complete program shows the situation:
static class ExtensionMethods {}
class Program {
static void Main() {
Test("Test");
}
public static bool Test(object obj)
{
return obj is ExtensionMethods;
}
}
I would have expected "obj is ExtensionMethods" to raise a warning of some sort, given that ExtensionMethods is a static class.
The compiler will issue a warning for the "is" operator when object under test can never be of the provided type, ((string)obj) is System.Uri
for example.
Am I forgetting a scenario in which this would actually be a meaningful test?