Type System Unification in C# .NET
Last Updated :
06 May, 2019
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 T
ype System Unification.
Example: All the predefined types (like short, int long etc.) are structs. Below are predefined types along with their
struct equivalents:
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.
csharp
// 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());
}
}
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:
csharp
// 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);
}
}
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:
csharp
// 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);
}
}
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.
Similar Reads
LINQ | Set Operator | Union
In LINQ, Set operators are those operators in query expression which return a result set which is based on the existence or non-existence of the equivalent elements within the same or different collections or sequences or sets. The standard query operator contains the following set operators: Union
3 min read
C# String Join() Method | Set - 1
In C#, Join() is a string method. This method is used to concatenate the members of a collection or the elements of the specified array, using the specified separator between each member or element. This method can be overloaded by passing different parameters to it.Concatenation: This method is use
5 min read
What is Reflection in C#?
Reflection is the process of describing the metadata of types, methods and fields in a code. The namespace System.Reflection enables you to obtain data about the loaded assemblies, the elements within them like classes, methods and value types. Some of the commonly used classes of System.Reflection
4 min read
Partial Methods in C#
C# contains a special method is known as a partial method, which contains declaration part in one partial class and definition part in another partial class or may contain both declaration and definition in the same partial class. Basically, partial methods exist in the partial class, or in the stru
2 min read
C# Restrictions on Properties
Properties in C# are special class members that provide a flexible mechanism to read, write, or compute the value of a private field. They act as methods called accessors i.e. "get" and "set" methods. Using Properties, we can achieve encapsulation and information hiding. Properties allow controlled
5 min read
C# | Multiple inheritance using interfaces
Introduction:Multiple inheritance refers to the ability of a class to inherit from multiple base classes. C# does not support multiple inheritance of classes, but it does supportmultiple inheritance using interfaces. An interface is a collection of abstract methods that a class can implement to prov
7 min read
C# Int16 Struct
In C#, Int16 Struct defined under the System namespace represents a 16-bit signed integer also known as a short datatype etc. We can also call the methods of math class to perform mathematical operations. Int16 struct inherits the ValueType class which inherits the Object class. Characteristics are
3 min read
C# Int32 Struct
In C#, the Int32 struct represents a 32-bit signed integer and is commonly used as the int data type. It belongs to the System namespace and provides various methods to perform operations like mathematical computations, parsing, and type conversion. The Int32 struct inherits from ValueType, which in
3 min read
C# | Generics - Introduction
Generic is a class which allows the user to define classes and methods with the placeholder. Generics were added to version 2.0 of the C# language. The basic idea behind using Generic is to allow type (Integer, String, ⦠etc and user-defined types) to be a parameter to methods, classes, and interfac
6 min read
C# | Type.GetNestedTypes() Method
Type.GetNestedTypes() Method is used to get the types nested within the current Type. There are 2 methods in the overload list of this method as follows: GetNestedTypes() Method This method is used to return the public types nested in the current Type. Syntax: public Type[] GetNestedTypes ();Return
5 min read