Common Type System in .NET Framework

Common Type System (CTS) in .NET Framework

In this article, I am going to discuss the Common Type System (CTS) in .NET Framework. Please read our previous article, where we discussed the Intermediate Language in .NET Framework with example.

What is the Common Type System in .NET Framework?

Common Type System (CTS) is nothing but a common data type platform where you write your code either in C# or in VB or in any dot net supported programming languages, all the data types of these languages get into a common data type platform which is known as Common Type System.

At the time of compilation, all language-specific data types are converted into CLR’s data type. This data type system of CLR which is common to all programming languages of .NET is known as CTS.

What we will do here is, we will create two applications. One is a C# Application and the other one is a VB.NET Application. And then we will try to see the IL code of both of these applications and then we will try to see how the CTS looks like.

Understanding CTS:

Here we are going to create two class library projects. One class library project using C# language and the other class library project using the VB language.

Creating a C# class library project:

Create a class library project with the name CsharpClassLibrary and using the C# programming language. Once you create the C# class library project then add a class called Calculator and then copy and paste the following code in it.

namespace CsharpClassLibrary
{
    public class Calculator
    {
        public int Calculate()
        {
            int a = 10, b = 20;
            int c = a + b;
            return c;
        }
    }
}
Creating VB Class Library Project:

To the same CsharpClassLibrary solution, let us add another class library project with the name as VBClassLibrary but using VB as the programming language. Once you created the VB Class library project then add a class called Calculator to this project and copy and paste the following code in it.

Public Class Calculator
    Public Function Calculate() As Integer
        Dim a As Integer = 10
        Dim b As Integer = 10
        Dim c As Integer
        c = a + b
        Return c
    End Function
End Class

Now, build the solution which should generate the respected .dlls. In our previous article, we discussed how to use the ILDASM tool to see the IL code in detail. So, let open Visual Studio Command Prompt and run two instances of the ILDASM.exe command i.e. one for VB dot net exe file and other for C# exe file as shown in the below image.

What is the Common Type System in .NET Framework?

Now, let us open the IL code of the Calculate method of both the class library project as shown in the below image. Please have a look at the integer variable in the IL code which is int32 in this case. In the C# class library project, we use int as the data type to declare variables a, b and c whereas in the VB class library project we use Integer as the data type to declare the variables a, b, and c. At the end of the day, both the data types are compiled to a common data type i.e. int32.

Common Type System (CTS) in .NET Framework

Whether you write the code in VB.NET or in C#.NET, if it following the dot net rules or specifications, at the end of the day it is compiled into a Common Type System (CTS).

We already demonstrate IL code and CTS. So, in the next article, I am going to demonstrate to you the example of Common Language Specification (CLS) in detail. Here, in this article, I try to explain the Common Type System in .NET Framework in detail and I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *