System.Object provides a ToString() method which gives a human readable string about the object. Many people haven’t realized how useful is this tiny method.
Consider the following code:
namespace PersonDetails
{
class Person
{
public Person(string firstName, string lastName, int age)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Age = age;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
Person chuckNorris = new Person("Chuck", "Norris", 20);
Console.WriteLine(chuckNorris.ToString()); // Prints PersonDetails.Person
}
}
}
The information printed is not at all helpful. Let us override ToString() and see the difference. (more…)
