What's So Common About the C# Common Type System (CTS)?

The wide spread range of the technologies that Microsoft .NET delivers isastonishing and astounding as it provides a way to knock down the barriers thathave traditionally divided developers into separate but unequal languageworlds. The combination of all these technologies, which is interesting on itsown, is even more compelling with the advent of a unifiedtype system called the Common Type System or just CTS.

This article introduces you to the CTS and its purpose. This articlehighlights the following:

Intro to the Common Type System(CTS)

The CTS provides every language running on the .NETplatform with a base set of data types. While the CTS isresponsible for defining the types that can be used across the .NET languages,most languages choose to implement aliases to those types. For example, afour-byte integer value is represented by the CTS type System.Int32. C# definesan alias for this called type called int.

Every thing in the CTS is an object. Not only iseverything an object but even more importantly, all objects implicitly derive from a single base class defined as a part of theCTS. This base class is called System.Object. Thedesigners of CTS were faced with the task of creating a type system in whichevery thing is an object, but the type system works in an efficient manner , when applicable. Their solution was to separate theCTS types into two categories:

  •  
    1. VALUE TYPES ( assigning actual values to the variables)
    2. REFERENCE TYPES (referencing to point to the address of the object)

Intro to the Common LanguageRuntime (CLR)

The high-performance CLR (Common Language Runtime)includes an

  •  
    • Execution engine,
    • A garbage collector,
    • Just-in-time (JIT) compiler,
    • A security system,
    • A rich class framework (The .Net framework).

The CLR was designed to support multiple languages.The CLR is responsible for managing the execution of code compiled for the .NETplatform. Code requiring the CLR at runtime in order to execute is referred toas " managed code". Compilers that targetthe .NET platform generate managed code that relies on a core set of servicesprovided by the CLR. Despite the fact that managed code is compiled to anintermediate language (IL), .NET programs are not interpreted by a"virtual machine". The CLR is responsible for compiling .NETapplications to native machine code. It is also technically possible to portthe CLR to a variety of hardware platforms, even other programming systems.Some of the salient features of CLR include:

  •  
    • Cross-language integration
    • Enhanced security
    • Versioning and deployment support
    • Debugging and profiling services
    • Memory management

Why the Need for a Common Runtimeand Type System?

Each programming language seems to bring its own datatypes with it. For example, VB represents strings usingthe BSTR struct, C++ offers char and wchar datatypes, and MFC offersthe CString class. The C++ intdatatype is a 32-bit value where the VB integer datatype, prior to VB .NET, is a 16-bit value. One of thegoals of the .NET platform is to make it easier for the developer to writeapplications. COM developers are exposed to a headache as they need to beconcerned about GUIDs, HRESULTs,early versus Late binding, managing reference counts,the type of threading model your component will run in, and proxy and stublayers etc.

Fortunately some of the programming environment andframeworks such as VB and ATL come to the rescue and reducesmuch of this complexity.For example, VB automaticallygenerates the appropriate GUIDS and also manages reference counts for COMobject used within the application. But still many developers will argue thatVisual Basic's implementation of COM leaves a lot to be desired. The solutionhas been presented in the form of creating such a system which is common to allthe languages related to this platform and so the common type system wascreated.

Common Language Specification(CLS)

The Common Language Specification describes acommon level of language functionality. The CLS is a set of rules that alanguage compiler must adhere to in order to create .NET applications that runin the CLR. Anyone who wants to write a .NET-compliant compiler needs simply toadhere to these rules and that's it. The relatively high minimum bar of the CLSenables the creation of a club of CLS-compliant languages. Each member of thisclub enjoys dual benefits: complete access to .NET framework functionality andrich interoperability with other compliant languages. For example a VB classcan inherit from a C# class and override its virtual methods.

Common Methords of the CTS:

Every type supported by CTS is derived from System.Object. Therefore, every type supports the followingmethods:

1. PUBLIC METHODS:

Boo
lean Equals(Object):

It is used to test equality with another object.Reference types should return true if the Object parameterreferences the same object and Values types when references the samevalue.

Int 32 Get Hash Code( ):

It generates a number corresponding to the value ofan object. If two objects of the same type are equal, then they must return thesame hash code. This value is used extensively by the sorting algorithmsimplemented in System.Collections.

Type GetType( ):

Gets a Type Object that can be used to accessmetadata associated with the type and as a starting point for navigating the objectheirarchy exposed by the Reflection API.

String ToString( ):

The default implementation returns the fullyqualified name of the class of the object. This method is often overridden tooutput data that is more meaningful to the type.

2. PROTECTED METHODS:

void finalize( ):

This method is called by the runtime to allow forcleanup prior to garbage collection.

Object MemberwiseClone:

This member represents a shallow copy of the objectcontaining references to other objects that does noyinclude copies of the objects referenced.

Set of Languages that Rarget the CLR

Microsoft provided languages that target the CLRinclude Visual Basic .NET, Visual C++ .NET with Managed Extensions, Visual C#.NET, and JScript .NET. Third parties are alsoproviding many other languages—-too many to list here.

CTS Benefits

The benefits of the Common Type System include:

Language interoperability:

Since all .NET languages are using a single typesystem, you are assured that objects and types are created in different languagescan interact with one another in a seamless manner. It's the CTS/CLScombination that helps make language interoperability more than just aprogrammer's dream.

Singly rooted object hierarchy:

A singly rooted object hierarchy is the key to aunified type system because it guarantees that each object in the hierarchy hasa common interface and therefore everything in the hierarchy will ultimately beof the same base type.

Type safety:

Type safety guarantees that types are declaredsafely and there is no way to trick the system to think that one type isactually another and only appropriate functions can be preformed on aparticular object.

Most Commented Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!