Introduction
Let us start by creating a new class, and we will leave it without any methods.
public class Employee
{
}
In the next step, we will try to use our class; we will find that our Employee class contains 4 methods which are (Equals, GetHashCode, GetType, and ToString).

The question now is, from where do these four methods come?
- The answer is easy: any data type in the .Net inherits implicitly from the Object class, which defines a common set of members supported by every type in the .NET, so when we create our new class Employee, it inherits from the Object class.
- The Object base class defines some members that are virtual, so it allows us to redefine the default implementation of these members.
- These virtual members give you good work, but in some cases, you will need a new implementation of these methods to fit your class functionality.
- In the next section, we will see how to use the default implementation of the Object class virtual methods, and then we will try to redefine it by using the override keyword to give it a new behavior.
What is the ToString() Method?
The ToString() method gives you a textual representation of the current state of the object.
Example
namespace UsingObjectClass
{
public class Employee
{
private string firstName;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
private string lastName;
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string employeeID;
public string EmployeeID
{
get { return employeeID; }
set { employeeID = value; }
}
}
class Program
{
static void Main(string[] args)
{
Employee emp1 = new Employee();
emp1.FirstName = "Amr";
emp1.LastName = "Ashush";
emp1.Age = 23;
string s = emp1.ToString();
Console.WriteLine(s);
}
}
}
Output

As you can see, the returned string is the name of the Employee Type (UsingObjectClass.Employee).
What if we want to use the ToString() method to show the current employee data (First Name, Last Name, and Age)?
All we need to do is to override the ToString() Method to give it a new behavior, so in our case, we will redefine it to show the Employee data instead of the name of the object type.
What is the Overriding ToString() Method?
The first step is to use the override keyword to override the method, and then we will redefine its behavior.
Example
public class Employee
{
// ...
// Overriding ToString() method
public override string ToString()
{
string s = "First Name: " + firstName + " , " + " last Name: " + lastName + " , " + "and Age: " + age;
return s;
}
}
Trying the new behavior of the ToString() method.
Employee emp1 = new Employee();
emp1.FirstName = "Amr";
emp1.LastName = "Ashush";
emp1.Age = 23;
string s = emp1.ToString();
Console.WriteLine(s);
output

As you can see, we now have a good text representation of our object.
We can use also StringBuilder in our ToString() method as follows.
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("First Name: {0}, Last Name: {1}, and Age: {2}", firstName, lastName, age);
return sb.ToString();
}
What is the Equals() Method?
The equals () method is used to compare two objects, and it returns a true if these objects point to the same values on the heap and returns a false if not.
Example
Employee emp1 = new Employee();
emp1.FirstName = "Amr";
emp1.LastName = "Ashush";
emp1.Age = 23;
Employee emp2 = emp1;
We now have two objects that point to the same value on the heap, so if we tried to use Equals() method, it will return true.
// This will return True.
emp1.Equals(emp2);




Arun KumarPosted Aug 5, 2011, 4:27 AM
I'm overriding Equals() method and not overriding GetHashCode() method but not getting any compile time error. Can you explain when this error occure.
Fatima SadiaPosted Jul 2, 2010, 2:37 PM
Hi Mr Amr Monjid i'm doing BS in computer science and i'm learning c# language. My question is about events that how the events work, and how we can declare customize ( our own ) events in .net framework.