BCA / B.Tech 9 min read

Common Type System

Common Types System in Hindi | Common Type System in Hindi:


In the .NET Framework, there are many types of data types that are used to store and manage different data. Mainly in .NET, there are two main types of types: Value Types and Reference Types.

Nullable Types: In .NET, Nullable Types can be used with Value Types so that they can accept a null value. For example, int? is a Nullable int, which can store a null value.

Dynamic Types: In .NET, dynamic is a special type that can store any type of data at runtime. This type does not have a fixed type for storing memory and its type is determined at runtime. Example:

dynamic value = "Hello";
value = 10; // valid because dynamic can change type at runtime
These types are used to store, copy, and manipulate any data in .NET.


Let's understand these types in detail:

1. Value Types

Value Types are those types that store the value directly. This means that the data stored in these types is placed directly in the stack and they are stored at different memory locations. If a Value Type is copied to another Value Type, then a copy of the value is made and they are independent.

Examples of Value Types:

Primitive Types: These types are used to store simple data, such as numbers and Boolean values. Some common Primitive Types are:

  • int (whole number): such as int a = 5;
  • float (decimal): such as float b = 3.14f;
  • double (large decimal): such as double c = 6.283;
  • bool (Boolean): such as bool isTrue = true;
  • char (character): such as char letter = 'A';
  • Structs: In .NET, a Structure (struct) is a value type in which different types of data can be stored in one place. For example, a Point can be a struct that stores x and y coordinates.

Enumerations: Enums are a special type of value type that stores a fixed set of named values. For example:

enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }

2. Reference Types

  • Reference Types store a reference to the actual value of the data in memory instead of the actual value itself. This means that when a Reference Type is copied to another Reference Type, the reference to that value is copied, not the actual value of the data.
  • Reference Types are stored on the Heap in memory, and they are automatically managed by the Garbage Collector.

Examples of Reference Types:

Classes: A class is a Reference Type that has properties, methods, events, and other members. When an instance of a class is taken, a new object is created that is stored at a separate location in memory. Example:

class Person {
    public string Name { get; set; }
    public int Age { get; set; }
}
Interfaces: An interface is a type of contract in which any class has to implement special methods. An interface is considered a Reference Type because it stores only the reference of any object that implements that interface.

Arrays: An array is a Reference Type. In this, a group of data of the same type is stored. Example:

int[] numbers = new int[5];

Delegates: A delegate is a Reference Type that stores a reference to methods. It is used in events and callback functions. Example:

public delegate void PrintMessage(string message);