Common Type System CTS in C#

The CTS is an integral part of the runtime and helps to support cross language communication.CTS specifies certain guidlines for declaring,using and managing types at runtime.
The functions performed by the CTS are :-

  • Provide cross language communication
  • Type Safety
  • High performance execution of code by estabilishing a framework
  • Supports an object oriented model for implementation of different programming languages
  • Specifies guidlines for different languages, ensuring the interaction between their objects
  • Supports primptive data types,such as Boolean,Byte,Char,and Int32 that are used in ,the process of development of an application

CTS can be classified into two datatypes :-

  • Value Type
  • Reference Type

Value Types in C#

Value Types :- The Value Type variables are managed by CLR and can store their values in either the Stack or Heap Memory.The garbage collector manages the memory allocated to these types by releasing the memory space of variables when they are no longer in use.An instance of a Value Type has its value allocated on the Stack.Variables declared as value Types have their own seperate copy of data and therefore any operation on one variable does not affect the other variables.
Value Types are variable that can be assigned a value directly.They are derived from System.ValueType and have a special behaviour in CLR. The value types and their contents are stored at the same location in memory.
The default values of Value Types are stored on Stack.
These are few Value Types :-

  • Bool
  • Byte
  • Char
  • Decimal
  • Double
  • float
  • int
  • long
  • Sbyte
  • Short
  • uint
  • ulong
  • ushort

Reference Types :- C# also provides types that are passed by references to calling functions, known as reference types. Variables that refers to the Objects,store the reference of the data not actual data.
In reference types, any changes made in the new copy midify the old copy to which object is referenced. The reference types use heap to store the references ,instead of the actual data.

C# provides some built in reference types which are follows :-
Dynamic Type :- Performs the type checking of the dynamic type variable at runtime instead of compile time. You can add any type of value in the dynamic type variable.
For Example
Dynamic dynObj=10;
Object Type :- Object Type enables you to assign values of any type to the variables of Object Type. Object is an alias for the predefined System.Object class.
The Object Type is the ultimate base class for the other types in Common Type System,such as value types,reference types, predefined types and user-defined types. All the types are Derived directly or indirectly from the Object Type.

What is Boxing ? When a Value Type is converted into the Object Type (reference type) , this process is known as Boxing.
For Example
Object X;
X=35; Here we assign X a value with 15 ,is called the Boxing.

What is Unboxing ? When a Object Type is converted into the Value Type , this process is known as Unboxing. The Reference Type can be declared using any of the keywords like Class,Interface,Delegates etc .The Reference Types also use built in reference types like Dynamic,Object,String .

Hierarchical Representation of Data Types

Image is not available

Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.