Junior · Explain · Runtime types and assignability direction

How do a declared reference type, GetType(), and IsAssignableFrom answer different questions?

Think through your answer before opening the explanation.

Runtime & concurrency

Reveal answer and reasoning

A declared reference type controls the source-level operations available through a variable, while GetType reports the runtime type of the actual nonnull object. If an Animal variable refers to a Cat, the variable remains declared Animal and the object remains a Cat; asking for metadata changes neither. IsAssignableFrom is a compatibility question on Type objects, with the receiver serving as the target and its argument serving as the source.

For the lesson's hierarchy, Animal accepts a Cat source, but Cat cannot accept every possible Animal source. That difference does not require constructing or casting another object. A common wrong answer reverses the operands or treats a matching simple type name as full identity. In a registry, keep compatibility separate from allowed construction and policy: an interface may accept a concrete class without being constructible itself, and compatible metadata alone does not authorize arbitrary code execution.

What a strong answer covers

  • Declared and runtime types differ.
  • The IsAssignableFrom receiver is the assignment target.
  • Compatibility does not establish construction or policy.

Common pitfall

Reading IsAssignableFrom backwards or treating Type.Name as complete identity.

Follow-up questions

  1. What happens when the object reference is null?
  2. Why are sibling classes not interchangeable merely because they share a base?

Self-review

Choose what to revisit. These marks are your own assessment and are separate from lesson completion.

Primary references

Open lesson and code ↗

← All interview questions