Type System Unification in C# .NET

The Type System Unification in C# means all the data types in C# are inherited from the Object class, whether directly or indirectly. Or you can say all types are considered as objects. In C#, primitive types are known as the Value Types which are basically structs. Internally, structs and classes inherit the Object Class. That’s why all types are indirectly considered as Objects, and this terminology is known as Type System Unification.

Example: All the predefined types (like short, int long etc.) are structs. Below are predefined types along with their struct equivalents:

Predefined Type Equivalent Struct
short System.Int16
int System.Int32
long System.Int64
ushort System.UInt16
uint System.UInt32
ulong System.Int64
byte System.Byte
sbyte System.SByte
bool System.Boolean
char System.Char
Float System.Single
Double System.Double
Decimal System.Decimal

Example: In the below program, the value data types such as int, char, bool are used as objects because of type system unification with the method ToString() that returns a string representing that particular object.



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of Type 
// System Unification
using System;
  
class GFG {
  
    // Main Method
    public static void Main(string[] args)
    {
        // Value data types
        int i = 1;
        char c = 'A';
        bool b = true;
  
        Console.WriteLine(i.ToString());
        Console.WriteLine(c.ToString());
        Console.WriteLine(b.ToString());
    }
}

chevron_right


Output:

1
A
True

Boxing and Unboxing

Type System Unification enables boxing and unboxing as any value data type can be treated as an object. So, when a value data type such as int, char, bool, etc. is converted into an object type, it is known as boxing. Conversely, when the object type is converted back into the data type, it is known as unboxing.

Boxing: A value data type such as int, char, bool, etc. is converted into an object type implicitly in boxing. First, an object type is allocated and then the value in the value data type is copied into the object type.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of Boxing
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
        // int value data type
        int val = 8;
  
        // boxing
        object obj = val;
  
        System.Console.WriteLine("val = {0}", val);
        System.Console.WriteLine("obj = {0}", obj);
    }
}

chevron_right


Output:

val = 8
obj = 8

Unboxing: An object type is converted into a value data type such as int, char, bool, etc. explicitly in unboxing. First, it is checked if the object type is the boxed value of the value data type. If yes, then the value in the object type is copied out of it.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of Unboxing
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
        // int value data type
        int val1 = 8;
  
        // boxing
        object obj = val1;
  
        // unboxing
        int val2 = (int)obj;
  
        Console.WriteLine("val1 = " + val1);
        Console.WriteLine("obj = " + obj);
        Console.WriteLine("val2 = " + val2);
    }
}

chevron_right


Output:

val1 = 8
obj = 8
val2 = 8

Benefits of Type System Unification: The Type System Unification is quite useful as it provides the dual benefit of a data type behaving as a value data type or an object type. A value data type can be used when required and it can be converted into an object type using boxing if required because of the inherent characteristic of Type System Unification.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.