O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required