Depending on your use case, "is" will not work properly. Take the Foo class derived from the Bar class. Create an obj object of type Foo. Both "obj - Foo" and "obj is Bar" will return true. However, if you use GetType () and compare with typeof (Foo) and typeof (Bar), the result will be different.
The explanation here and here is a snippet of source code demonstrating this difference:
using System; namespace ConsoleApp { public class Bar { } public class Foo : Bar { } class Program { static void Main(string[] args) { var obj = new Foo(); var isBoth = obj is Bar && obj is Foo; var isNotBoth = obj.GetType().Equals(typeof(Bar)) && obj.GetType().Equals(typeof(Foo)); Console.Out.WriteLine("Using 'is': " + isBoth); Console.Out.WriteLine("Using 'GetType()': " + isNotBoth); } } }
source share