What is the base class for all data types in C#.NET?

Object is the base class for all data types in C#. The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). The object is an alias for System.Object class.

When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.

Syntax

Following is the syntax for declaring an object variable −

object variableName;
object variableName = value;

Following is the syntax for boxing and unboxing −

// Boxing: value type to object
object obj = 10;

// Unboxing: object to value type
int num = (int)obj;

Understanding the Object Class Hierarchy

Every type in C# ultimately derives from System.Object, whether directly or indirectly. This includes both value types and reference types.

C# Type Hierarchy System.Object Value Types Reference Types int, float, bool char, struct string, array class, interface

Using Object Data Type

Example

using System;

namespace Demo {
   class ObjectClass {
      public int x = 56;
   }

   class MyApplication {
      static void Main() {
         object obj;
         obj = 96;
         Console.WriteLine(obj);
         obj = new ObjectClass();
         ObjectClass newRef;
         newRef = (ObjectClass)obj;
         Console.WriteLine(newRef.x);
      }
   }
}

The output of the above code is −

96
56

Boxing and Unboxing Example

Example

using System;

class Program {
   static void Main() {
      // Boxing: Converting value type to object
      int num = 25;
      object obj = num;
      Console.WriteLine("Boxed value: " + obj);
      Console.WriteLine("Type: " + obj.GetType());

      // Unboxing: Converting object back to value type
      int unboxedNum = (int)obj;
      Console.WriteLine("Unboxed value: " + unboxedNum);

      // Boxing with different data types
      object boolObj = true;
      object charObj = 'A';
      object stringObj = "Hello";

      Console.WriteLine("Bool: " + boolObj);
      Console.WriteLine("Char: " + charObj);
      Console.WriteLine("String: " + stringObj);
   }
}

The output of the above code is −

Boxed value: 25
Type: System.Int32
Unboxed value: 25
Bool: True
Char: A
String: Hello

Common Object Methods

Since all types inherit from System.Object, they all inherit these common methods −

Example

using System;

class Student {
   public string Name { get; set; }
   public int Age { get; set; }

   public Student(string name, int age) {
      Name = name;
      Age = age;
   }

   public override string ToString() {
      return $"Student: {Name}, Age: {Age}";
   }
}

class Program {
   static void Main() {
      Student student = new Student("John", 20);
      
      // Using inherited Object methods
      Console.WriteLine("ToString(): " + student.ToString());
      Console.WriteLine("GetType(): " + student.GetType());
      Console.WriteLine("GetHashCode(): " + student.GetHashCode());
      
      // Comparing references
      Student student2 = student;
      Console.WriteLine("ReferenceEquals: " + ReferenceEquals(student, student2));
   }
}

The output of the above code is −

ToString(): Student: John, Age: 20
GetType(): Student
GetHashCode(): 58225482
ReferenceEquals: True

Conclusion

System.Object is the ultimate base class for all data types in C#, providing common functionality like ToString(), GetType(), and Equals(). Understanding boxing and unboxing is essential when working with value types as objects, as it affects performance and behavior in your applications.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements