Understanding Relationship Between CTS and CLS
CTS and CLS are parts of .NET CLR and are responsible for type safety within the code. Both allow cross-language communication and type safety. In this article, I would like to expose the relationship between these two.
CTS stands for Common Type System. It defines the rules which Common Language Runtime follows when declaring, using, and managing types. The CTS generally deals with the data types. So normally we have access to multiple languages and each and every language has its own set of rules and tweaks regarding the data type where the one language's data type cannot be interpreted by other languages but the .NET Framework language would be able to understand all the data types certainly.
The common type system performs the following functions:
It enables cross-language integration, type safety, and high-performance code execution.
It provides an object-oriented model for the implementation of many programming languages.
It defines rules that every language must follow which runs under the .NET framework. It ensures that objects are written in different .NET languages like C#, VB.NET, F#, etc. can interact with each other.
CLS stands for Common Language Specification and it is a subset of CTS. It defines a set of rules and restrictions that every language must follow which runs under the .NET framework. The languages which follow this set of rules are said to be CLS Compliant. In simple words, CLS enables cross-language integration.
The CLS is a specification that defines the rules for supporting the language integration in a certain way that the programs are written in any language, still, it can interoperate with the one another seamlessly while taking the full advantage of concepts such as exceptions handling, inheritance, polymorphism, and other features accordingly. These CLS rules and the specification are documented in the ECMA proposed standard document.
For example, one rule is that you cannot use multiple inheritances within .NET Framework. As you know C++ supports multiple inheritances but; when you will try to use that C++ code within C#, it is not possible because C# doesn’t support multiple inheritances.
One another rule is that you cannot have members with the same name with case difference only i.e. you cannot have to add() and Add() methods. This easily works in C# because it is case-sensitive but when you will try to use that C# code in VB.NET, it is not possible because VB.NET is not case-sensitive.
Why CTS is Called Common Type System?
In .NET, every Data Type is internally represented by a class or structure. All the classes and structures related to Data Types are collectively known as CTS. As you know every language provides its own keywords for Data Types but internally all the languages which run under the .NET framework use the classes and structures available in CTS.
For example, C# has int Data Type and VB.Net has Integer Data Type. Hence a variable is declared as an int in C# or Integer in vb.net, finally, after compilation, use the same structure Int32 from CTS.
All the structures and classes available in CTS are common for all .NET Languages and the purpose of these is to support language independence in .NET. Hence it is called CTS.
The CTS is meant for declaring the different data types, along with the specification like how the types are managed in runtime with the cross language integration, type safety with great performance execution whereas CLS is meant for the different language interoperability it means the programs that is written in one specific language can be communicated with any other language, while having the full advantages of all available object-oriented specifications such as Polymorphism, Inheritance, etc.
What do you think?
I hope, you have enjoyed the article about CTS and CLS. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
Take our free skill tests to evaluate your skill!
In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.