Override Basic Object In C#

Introduction

 
We all know Object is the base class of all .NET classes and by default it is inherited to all .Net Class. It's either inbuilt or custom class. That’s why when we type
“<anyobject>. “ we will be able to see 4 methods as shown in the below methods, even though our defined class is empty.
 
 
Below is the snapshot of object class,
 
 
As we can see in the above code snap, we can override 3 methods  if it is required; i.e. if our requirement demands or if you think by overriding these methods, we can make the best use of it.
 
We will see this with an example.
 
We will create Employee class in this:
  • We will be able to get the ID of employee using method GetHashCode
  • We will be able to get employee name just be converting employee object as string.
  • We will able to compare the employee id by Equals method
For the above mentioned requirement below is the sample code:
  1. using System;  
  2.   
  3. namespace Sample.Basic.Object  
  4. {  
  5.     public class Employee  
  6.     {  
  7.         public Employee(string name, int id, double salary)  
  8.         {  
  9.             Name = name;  
  10.             Id = id;  
  11.             Salary = salary;              
  12.         }  
  13.         private string Name { getset; }  
  14.         private double Salary { getset; }  
  15.         private int Id { getset; }  
  16.         public override bool Equals(object obj) => obj.GetHashCode() == Id;  
  17.         public override int GetHashCode() => Id;  
  18.         public override String ToString() => Name;  
  19.   
  20.     }  
  21. }  
In the above code we can see that we override the GetHashCode , Equals and ToString Method.
  • In GetHashCode we added code to get employee ID.
  • In ToString we added code to get employee Name.
  • In Equals we added code to compare the employee object only with ID as reference.
In Main method we will use the Employee object as below, to see the output as per our above requirement.
  1. using static System.Console;  
  2. namespace Sample.Basic.Object  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.   
  9.             Employee employeeA = new Employee("ABC", 111, 100000);  
  10.             Employee employeeB = new Employee("XYZ", 112, 100000);  
  11.             WriteLine($"employeeA Name: {employeeA.ToString()}");  
  12.             WriteLine($"employeeA ID: {employeeA.GetHashCode()}");  
  13.             WriteLine("-------------------------------------------------");  
  14.   
  15.             WriteLine($"employeeB Name: {employeeB.ToString()}");  
  16.             WriteLine($"employeeB ID: {employeeB.GetHashCode()}");  
  17.             WriteLine("-------------------------------------------------");  
  18.   
  19.             WriteLine(employeeA.Equals(employeeB));  
  20.             WriteLine(employeeA.Equals(employeeA));  
  21.   
  22.             ReadLine();  
  23.         }  
  24.     }  
  25. }  
Below is the output snap of the code,
 
 

Summary

 
From this article we learned that we can override the 3 methods of basic object. At the end it is up to our knowledge to go for overriding it or not. As I told you in the beginning, if you think by overriding these methods that we can make best use of it, then  we can override these methods.


Recommended Free Ebook
Similar Articles