I have a question about the following code:
class CurrentDate
{
static void Main()
{
Console.WriteLine(DateTime.Now);
}
}
Documentation says:
Writes the text representation of the specified array of objects, followed by the current line terminator, to the standard output stream using the specified format information.
So my question is: How come WriteLine
knows the text representation of DateTime
object? I mean, if I create my own object from my own class, how would it know how to convert the value to text? And even more, how does it know what the value is? How can you define "value" of an object?
MyClass.toString
) to figure out what to print as the value when the object is displayed as a single row. – HuzzahConsole.WriteLine()
implicitly callsToString()
, so for your own object you have to implement/overrideToString()
method. – Chicoine