Understanding CTS (Common type system)
Understanding Common Language Specification (CLS)
Demonstration of CLS
When you start learning .NET and C# the first thing you come across is CLS and CTS concept. In MSDN and other articles you will just find artificial definitions like CLS is a set of rules or CTS is a common data type but no one really goes in to root of why these things have first come in place.
In this article with my little experience and knowledge will try to communicate the same in a demonstrative and practical way.
.NET framework supports multiple languages like c#, VB.Net , VF.NET etc. And it’s very much possible that we are coding in C# and we want to consume code of VB.NET. So in that scenario we need to ensure that this integration between cross platform languages work seamlessly.
The problem with the cross communication is data types of the languages. For example in C# we define
int num1 = 5;
int num2 = 2;
And in VB.NET
Dim num1 AsInteger
Now the problem is these are two different languages and the memory allocation for the data types can be very different. So when call is made from C# to VB.NET we can have integration issues this is where CTS comes to rescue.
So CTS ensures that when you compile in these disparate languages like C# and VB.NET it compiles them to a uniform data type.
For example integer of VB.NEt and Int in C# becomes int32. Below is a simple screen shot where we have VB.NET code which is using integer and C# code which is using int , but if you open in ILDASM you will see its “int32”.
When it comes to communication between languages other than data types we also need to take in to account the local genes, local behavior and local nature of those languages. For example C# is case sensitive and VB.NET is not case sensitive.
So now in order to achieve cross communication between these languages we need to ensure that they drop their local behavior and only support a common behavior or we can say a COMMON SPECIFICATION.
CLS a.k.a common language specifications are set of rules which if followed by a code will not have issues when called from other .NET supported languages. So if a C# code is following CLS then that code can be called from VB.NET , VF.NET or VC++.NET seamlessly and without issues.
You can read the long list of CLS rules from this link :-https://msdn.microsoft.com/en-us/library/12a7a7h3(v=vs.100).aspx
Below is a simple demonstration of CLS. Below is simple C# code which has two classes, one class is named as “Class1” and other is name is “class1” (in small case).
public class Class1
public class class1
If you try to consume that in VB.NET you do not see the above classes.
This is because VB.NET is case sensitive and is getting confused between “Class1” and “class1”.
Now let’s correct one class name to “class123” and VB.NET shows both the classes up.Why ??
Because now your C# code is complying the CLS rules of naming and case-sensitiveness.
In case you are completely new to .NET and C# I would suggest to start the below YouTube learn C# video series which I have followed as a fresher and has helped me a lot.