C# Type check fails unexpectedly - C#