At the very beginning of your C# journey, you learned about
Console.WriteLine(). You used this tool with nearly every type, from
bool b = true; Diary d = new Diary(); Console.WriteLine(b); Console.WriteLine(d);
We can use this same tool with every type because every type inherits from
Under the hood,
ToString(), which is defined in
Object. Every object needs some kind of
string representation to be printed in text. These two lines are equivalent:
Let’s prove to ourselves that
ToString() is used when printing to the console.
First, override the
ToString() method in the
Diary class. Have the method return the string
Back in Program.cs, construct a
Diary object and pass it directly to
You should see your surprise printed!