Assignment Compatibility

As the concepts of object types and interface types have now been explained, albeit very simply, the question of assignment compatibility can be addressed. A simple definition of assignment compatibility for reference types is that a location of type T, where T may be either an object type or an interface type, can refer to any object:

  • With an exact type of T, or

  • That is a subtype of T, or

  • That supports the interface T.

Listing 2.10 demonstrates aspects of assignment compatibility. The program starts by creating values of two different types and two interface references:

  • A value of type System.Int32 called i

  • An Object reference called o

  • A String reference called s

  • An IComparable reference called ic

Listing 2.10. Assignment ...

Get Programming in the .NET Environment now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.