C Sharp

Type Safety

The last benefit of the CTS that I want to mention is type safety. Type safety guarantees that types are what they say they are and that only appropriate operations can be performed on a particular type. Type safety provides a number of advantages and capabilities-as described-most of which stem from the singly rooted object hierarchy.

  • Every reference to an object is typed, and the object it's referencing is also typed. The CTS guarantees that a reference always points to what it implies it points to.

  • Because the CTS tracks every type in the system, there's no way to trick the system into thinking that one type is actually another. This is obviously a major concern for distributed applications in which security is a priority.

  • Each type is responsible for defining the accessibility of its members by specifying an access modifier. This is done on a member-by-member basis and includes allowing any access (by declaring the member public), limiting the visibility to inherited classes only (by declaring the member protected), allowing no access at all (by declaring the member private), and allowing access only to other types in the current compilation unit (by declaring the member internal). I'll discuss access modifiers in more detail in the next chapter.


Summary

The Common Type System is an important feature of the .NET Framework. The CTS defines the type system rules that applications must follow to run properly in the CLR. CTS types are divided into two categories: reference typesand value types . Namespaces can be used to define scope in an application.The benefits of a common type system include language interoperability, a singly rooted object hierarchy, and type safety. Types can be converted in C# through boxing and unboxing, and compatible types can be made to share characteristics and functionality through casting.

by BrainBellupdated
Advertisement: