Hi
I wonder whether there are advantages of overriding ToString() method. If yes, which ones?.
This code produces 3 times the same output, so why overriding the ToString() method?
Thanks
using System;
class Program
{
static void Main(string[] args)
{
Employee emp = new Employee() { Name = "Luke", Age= 40 };
Console.WriteLine(emp.ToString());
Console.WriteLine(emp.Name + " " + emp.Age);
emp.MyMethod();
}
}
public class Employee
{
public string Name;
public int Age;
public override string ToString()
{
return Name + " " + Age;
}
public void MyMethod()
{
Console.WriteLine(Name + " " + Age);
}
}

Sachin SinghRank #66 · 26.2K reputationPosted Jul 13, 2021, 4:00 PM EDT
Anant VernekarRank #1,055 · 1.8K reputationPosted Jul 13, 2021, 2:16 PM EDT
Valerie MeunierRank #1,527 · 1K reputationPosted Jul 13, 2021, 5:10 PM EDT