Introducing C# and the .NET Framework - The Common Type System
(Page 7 of 12 )
When Microsoft designed the .NET technology, they wanted to invent a technology that would last for some time with great stability and interoperability. When you think about most programming languages, you will find that a particular programming language consists of a set of keywords associated with a specific syntax. Attached to it are a group of data types like integer numbers, strings and floating point numbers. Most programming languages offer these data types, and most of them use the concepts of encapsulating the code into methods and, as a result, into classes.
This is good, but because each language defines its own data types and its own syntax, it's difficult to use a component written in another language in your code, although COM gave us this feature to some extent. You could write a COM Component in Visual Basic and use it from a scripting language like VB Script, but you can't unify the way all programming languages use each others' components -- for example, you can't create a COM Component in C++ and make use of it in a Visual Basic program.
The solution to this problem involves separating the implementation of most of the popular programming languages' data types into a unified system of syntax that can be used to operate on these data types. Microsoft has designed a Common Type System (or CTS) that defines most of the data types that have been used by most popular programming languages. The Common Type System doesn't define the syntax that programming languages use, nor does it define the keywords; it just defines the Common Data Types for all the languages that will be managed by the .NET CLR Environment.
Different languages may use a subset of the CTS called the Common Language Specifications (or CLS) to comply with the .NET Framework (we will discuss the CLS in the next section). The most important thing that you need to understand is that the CTS doesn't define any syntax, and it's the part of the programming language to define it. I think that by now you need a definition for the CTS.
The Common Type System: The CTS defines the rules for Types that run and can be managed by the Common Language Runtime for compilers and programming languages designers, so they can develop compilers to utilize these types and thus produce MSIL code that can be run under the CLR.
If you ever code in C# you may ask yourself what data type to use --the ones introduced by the C# Language or the set introduced by the CTS? The answer is very simple. .NET programming languages expose their data types (that's a subset of the CTS) in their own way. For example, the CTS defines a 4-byte Type, Int32, that stores integer numbers from negative 2,147,483,648 through positive 2,147,483,647. In C# you can use the int keyword instead of the CTS official name System.Int32 to declare a variable of that type. Actually you can use any of these techniques, but for consistency it is best to use one technique throughout your code. Personally I use the C# aliases. The table below lists the CTS built-in types and their C# aliases keywords.
| Description | C# Keyword | CTS Type |
| The base class for all class in the .NET | object | System.Object |
| Signed 4 bytes | int | System.Int32 |
| Unsigned 4 bytes | uint | System.UInt32 |
| Signed 2 bytes | short | System.Int16 |
| unsigned 2 bytes | ushort | System.UInt16 |
| a character string value | string | System.String |
| Signed 1 byte | byte | System.Byte |
| Unsigned 1 byte | sbyte | System.Sbyte |
| Signed 8 bytes | long | System.Int64 |
| Unsigned 8 bytes | ulong | System.UInt64 |
| Two bytes Unicode character | char | System.Char |
| four bytes float | Float | System.Single |
| eight bytes float | double | System.Double |
| sixteen bytes types exact to 29 digits. It's a requirement for banking and financial applications | decimal | System.Decimal |
| Boolean value, true or false | bool | System.Bolean |
A full discussion about types will have to wait until the next article. In that article, I will talk about why understanding types is critical for understanding C#. We will discuss Value Types and Reference Types and their mapping to the Common Type System. Value Types such as Built-In and Structures, and Reference Types such as Classes, Interfaces, Delegates, Arrays and Strings will be discussed then as well.
Next: A World of Interoperability Through the CLS >>
More C# Articles
More By Michael Youssef