Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
The Object Class in C#
The Object class is the base class of all classes in C# and the .NET Framework. Every class in C# implicitly inherits from the Object class, which means all objects have access to the fundamental methods provided by this base class.
When you create any class in C#, it automatically inherits from Object even if you don't explicitly specify it. This provides a common set of methods that every object can use.
Syntax
Every class implicitly inherits from Object −
public class MyClass {
// This class inherits from Object automatically
}
// Equivalent to:
public class MyClass : Object {
// Explicitly inheriting from Object
}
Object Class Methods
The Object class provides the following essential methods that every C# object inherits −
| Method | Description |
|---|---|
| Equals(Object) | Determines whether the specified object is equal to the current object. |
| Equals(Object, Object) | Determines whether the specified object instances are considered equal. |
| Finalize() | Allows an object to try to free resources and perform cleanup operations. |
| GetHashCode() | Returns the default hash code for the current object. |
| GetType() | Gets the Type of the current instance. |
| MemberwiseClone() | Creates a shallow copy of the current Object. |
| ReferenceEquals(Object, Object) | Determines whether the specified Object instances are the same instance. |
| ToString() | Returns a string that represents the current object. |
Using Object Class Methods
Example
using System;
public 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}";
}
public override bool Equals(object obj) {
if (obj is Student other) {
return Name == other.Name && Age == other.Age;
}
return false;
}
public override int GetHashCode() {
return Name.GetHashCode() ^ Age.GetHashCode();
}
}
public class Program {
public static void Main() {
Student s1 = new Student("John", 20);
Student s2 = new Student("John", 20);
Student s3 = s1;
Console.WriteLine("ToString(): " + s1.ToString());
Console.WriteLine("GetType(): " + s1.GetType());
Console.WriteLine("GetHashCode(): " + s1.GetHashCode());
Console.WriteLine("s1.Equals(s2): " + s1.Equals(s2));
Console.WriteLine("ReferenceEquals(s1, s2): " + Object.ReferenceEquals(s1, s2));
Console.WriteLine("ReferenceEquals(s1, s3): " + Object.ReferenceEquals(s1, s3));
}
}
The output of the above code is −
ToString(): Student: John, Age: 20 GetType(): Student GetHashCode(): 1248104350 s1.Equals(s2): True ReferenceEquals(s1, s2): False ReferenceEquals(s1, s3): True
Commonly Overridden Methods
Example
using System;
public class Product {
public string Name { get; set; }
public decimal Price { get; set; }
public Product(string name, decimal price) {
Name = name;
Price = price;
}
// Override ToString() for meaningful string representation
public override string ToString() {
return $"{Name} - ${Price:F2}";
}
// Override Equals() for value-based comparison
public override bool Equals(object obj) {
return obj is Product product &&
Name == product.Name &&
Price == product.Price;
}
// Override GetHashCode() when overriding Equals()
public override int GetHashCode() {
return HashCode.Combine(Name, Price);
}
}
public class Program {
public static void Main() {
Product p1 = new Product("Laptop", 999.99m);
Product p2 = new Product("Laptop", 999.99m);
Console.WriteLine("Product 1: " + p1);
Console.WriteLine("Product 2: " + p2);
Console.WriteLine("Are they equal? " + p1.Equals(p2));
Console.WriteLine("Same reference? " + ReferenceEquals(p1, p2));
}
}
The output of the above code is −
Product 1: Laptop - $999.99 Product 2: Laptop - $999.99 Are they equal? True Same reference? False
Object as Universal Type
Example
using System;
public class Program {
public static void PrintObject(object obj) {
Console.WriteLine("Type: " + obj.GetType().Name);
Console.WriteLine("Value: " + obj.ToString());
Console.WriteLine("Hash: " + obj.GetHashCode());
Console.WriteLine("---");
}
public static void Main() {
PrintObject(42);
PrintObject("Hello World");
PrintObject(true);
PrintObject(new DateTime(2024, 1, 15));
}
}
The output of the above code is −
Type: Int32 Value: 42 Hash: 42 --- Type: String Value: Hello World Hash: -1989457895 --- Type: Boolean Value: True Hash: 1 --- Type: DateTime Value: 1/15/2024 12:00:00 AM Hash: -1948092588 ---
Conclusion
The Object class is the root of the C# type hierarchy, providing essential methods like ToString(), Equals(), and GetHashCode() that every object inherits. Understanding and properly overriding these methods is crucial for creating well-behaved custom classes that integrate seamlessly with the .NET Framework.
