What’s Type Safety?
To enforce code to deal with types in a correct manner, the CLR guarantees type safety. This ensures objects can only be accessed in a manner that’s consistent with the object’s type contract. Types consist of two things: the data that’s contained by objects of the type (for example, a customer’s name and credit balance) and the operations you can perform on that data (for example, DiscountPrice on a Product).
To illustrate type-unsafe operations, assume you were able to treat an object of type Customer as if it were of type Product. Then, what would it mean to perform an operation DiscountPrice to the customer object? Maybe you’d be changing the customer’s credit balance. This is clearly an unsafe thing to do. Similarly, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access