Common Type System

From Wikipedia, the free encyclopedia

The Common Type System (CTS) is used by every language built on the .NET Framework. A fundamental part of the .NET Framework is Common Language Runtime (CLR), the CTS specifies no particular syntax or keywords, but instead defines a common set of types that can be used with many different language syntaxes. Each language is free to define any syntax it wishes, but if that language is built on the CLR, it will use at least some of the types defined by the CTS. While the creator of a CLR-based language is free to implement only a subset of the types defined by the CTS. Visual Basic.NET, C#, and pretty much every other language used with the .NET Framework rely heavily on the CTS.

Contents

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

Everything in the CTS is an object. Not only is everything an object but even more importantly, all objects implicitly derive from a single base class defined as a part of the CTS. This base class is called System.Object. The designers of CTS were faced with the task of creating a type system in which every thing is an object, but the type system works in an efficient manner, when applicable.

Each programming language tends to bring its own datatypes with it. For example, VB represents strings using the BSTR struct, C++ uses arrays of char/wchar_t or the std::string class, and Microsoft Foundation Classes uses the CString class. The int datatype in 32-bit versions of Visual C++ is a 32-bit value where the Visual Basic integer datatype, prior to VB.NET, is a 16-bit value.

One of the goals of the .NET platform is to make it easier for the developer to write applications. COM developers are exposed to a headache as they need to be concerned about GUIDs, HRESULTs, early versus Late binding, managing reference counts, the type of threading model your component will run in, and proxy and stub layers etc. Fortunately, some of the programming environment and frameworks such as Visual Basic and Active Template Library come to the rescue, reducing much of this complexity. Visual Basic, for example, automatically generates the appropriate GUIDs and manages reference counts for Component Object Model objects used within the application. Still, many developers will argue that Visual Basic's implementation of COM leaves a lot to be desired. The solution has been presented in the form of creating such a system which is common to all the languages related to this platform and so the common type system was created.

  • Establishes a framework that helps enable cross-language integration, type safety, and high performance code execution.
  • Provides an object-oriented model that supports the complete implementation of many programming languages.
  • Defines rules that languages must follow, which helps ensure that objects written in different languages can interact with each other.

The common type system supports two general categories of types:

Value types 
Value types directly contain their data, and instances of value types are either allocated on the stack or allocated inline in a structure. Value types can be built-in (implemented by the runtime), user-defined, or enumerations.
Reference types 
Reference types store a reference to the value's memory address, and are allocated on the heap. Reference types can be self-describing types, pointer types, or interface types. The type of a reference type can be determined from values of self-describing types. Self-describing types are further split into arrays and class types. The class types are user-defined classes, boxed value types, and delegates.

The following example shows the difference between reference types and value types:

Imports System

Class Class1
    Public Value As Integer = 0
End Class 'Class1

Class Test    
  Shared Sub Main()
    Dim val1 As Integer = 0
    Dim val2 As Integer = val1
    val2 = 123
    Dim ref1 As New Class1()
    Dim ref2 As Class1 = ref1
    ref2.Value = 123
    Console.WriteLine("Values: {0}, {1}", val1, val2)
    Console.WriteLine("Refs: {0}, {1}", ref1.Value, ref2.Value)
  End Sub 'Main
End Class 'Test

The output of the above example

Values: 0, 123
Refs: 123, 123


Boxing

convert ValueTypes to Reference Types also known as boxing. Lets see a small example below. You see in the example I wrote "implicit boxing" which means you don't need to tell the compiler that you are boxing Int32 to object because it takes care of this itself although you can always make explicit boxing as seen below right after implicit boxing.

       Int32 x = 10; 
       object o = x ;  // Implicit boxing
       Console.WriteLine("The Object o = {0}",o); // prints out 10
       //-----------------------------------------------------------
       Int32 x = 10; 
       object o = (object) x; // Explicit Boxing
       Console.WriteLine("The object o = {0}",o); // prints out 10

Unboxing

Lets now see UnBoxing an object type back to value type. Here is a simple code that unbox an object back to Int32 variable. First we need to box it so that we can unbox.

       Int32 x = 5; 
       object o = x; // Implicit Boxing
       x = o; // Implicit UnBoxing     


Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.