Stop Guessing Types in C#: typeof, GetType and IsAssignableFrom Explained
Let's break down the real jobs of typeof, object.GetType and Type.IsAssignableFrom with small, runnable examples and a few nerdy references for fun. You will learn when to reach for each API, why EF Core and infrastructure code lean on assignability checks, and how pattern matching fits into the story. We will cover null pitfalls, open generic comparisons and proxy types. By the end, your type checks will read clearly and behave predictably.
If you have ever stared at a line that mixes typeof, GetType and IsAssignableFrom and thought your code was speaking in riddles, you are not alone. I once spent a late night chasing a bug where two classes looked identical but behaved like siblings arguing over who touched the thermostat. The fix landed when I stopped treating type checks like a guessing game and learned what each tool really asks the runtime.
Meet the cast of characters
Think of typeof as the cast list, GetType as the actor on stage, and IsAssignableFrom as the casting rules. Each plays a different role, and mixing them up can turn your tidy drama into improv night.