Search Placementyogi.com
 

Common Type System (CTS) in C#

The common type system defines how types are declared, used and managed by all .NET-aware languages in common language runtime (CLR- a topic to be discussed later).

Few important points of CTS:

  • The members of an assembly file may be of these five standard common types such as {class, structure, interface, enumeration, delegate}. 
  • CTS defines standard rules that all .NET-aware languages must follow in order to interact with each other using their objects.
  • CTS provides a library that contains primitive data types such as {Byte, Char, Boolean etc.} for application development.

Let's discuss each one of them in short to have better understanding.

1. Class Type in C#

A class is the basic need for object-oriented programming. A class can contain methods as well as data members. A class is declared using keyword class. Now you can understand category bird as a class, flying as a method and color as data member. Now it can be put into a class type as below:

// C# class type
public class Bird
{
	string color1 = "red";
	string color2 = "green";
	public void Fly()
	{ ... }
}

2. Structure Type in C#

A structure is similar to the structures in C language but not exactly same. We can think structure as a lightweight class which can contain methods, fields and constructors just like class. It is mostly used to model geometric and mathematical data and declared using keyword struct.

// C# Structure Type
struct Circle
{
	//Field of structure
	public int radius;

	// Constructor of structure
	public Circle(int r)
	{ radius = r }

	//Method of structure to display
	public void DrawCircle()
	{ Console.WriteLine("{0}", radius); }
}

3. Interface Type in C#

Interface Type: An interface can contain only a collection of method names without its implementation. If any structure or class implements it, the same name can be extended and implemented based on current requirement. An interface is declared using keyword interface.

// C# interface type
public interface ICar
{
	AirConditionerController();
}

While handing over a car controlling program to a car company, Programmers can provide an interface with method name AirConditionerController() so that car company can decide and implement the type of controlling they want by implementing it.

4. Enumeration Type in C#

An enumeration is an effective way to define integral constants (like integers) to variables. For example if we take seven days of a week, only seven numbers (0 to 6) would be sufficient to define all days. Consider the following program to understand this concept better:

// C# enumeration type
using System;
namespace PlacementYogi
{
    class EnumerationTutorial
    {
        public static void Main(String[] args) 
        {
            Days today = Days.Monday;
            int dayNumber = (int)today;
            Console.WriteLine("{0} is day number #{1}.", today, dayNumber);
        }
    }

    public enum Days
    {
        Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
    }
}
// Output:
// Monday is day number #1.

5. Delegate Type in C#

A delegate in .NET is a class derived from System.MulticastDelegate. It is just like a C-style function pointer. Delegates are useful for

implementing events and forwarding a call from one entity to another entity and are declared using keyword delegate.

// C# delegate type 
public delegate int Operation(int a, int b);

This delegate can point to any method which has the same signature in C#.

 

Common Type System (CTS) Type Members

We have seen that there are five CTS types. Now these five types can contain members from the set {constructor, finalizer, static constructor, nested type, operator, method, property, indexer, field, read only field, constant, event} called CTS type members.

Common Type System (CTS) Data Types

All .NET-aware languages have their unique keywords which are used to declare standard CTS data types. Whatever datatypes we use in our program, they all are resolved to the CTS data types available in assembly mscorlib.dll.

The below table provides the CTS data types along with the corresponding keywords in various .NET-aware languages.

CTS Data Type VB.NET Keyword C# Keyword Managed Extensions for C++ Keyword
System.Byte Byte byte unsigned char
System.SByte SByte sbyte signed char
System.Int16 Short short short
System.Int32 Integer int int or long
System.Int64 Long long _int64
System.UInt16 UShort ushort unsigned short
System.UInt32 UInteger uint unsigned int or unsigned long
System.UInt64 ULong ulong unsigned _int64
System.Single Single float Float
System.Double Double double Double
System.Object Object object Object^
System.Char Char char wchar_t
System.String String string String^
System.Decimal Decimal decimal Decimal
System.Boolean Boolean bool Bool
Banner to LogicGuns.com

Hide Page Information

CTS (Commom Type System) Tutorial in C#, Learn basics and types of CTS in .NET, Helpful for Technical Interviews