C#C
C#3y ago
Sonath

Why does the following prints "Point" to the console instead of "(2, 3)"?

Point thing = new Point(2, 3);
System.Console.WriteLine(thing); // prints Point
System.Console.WriteLine(thing.ToString()); // prints (2, 3)

public class Point
{
    public int X {get; private set; }
    public int Y {get; private set; }

    public Point(): this(0, 0)
    {

    }

    public Point(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }

    // using NEW not OVERRIDE
    public new string ToString()
    {
        return $"({X}, {Y})";
    }
}

Is it because Console.WriteLine uses an object reference internally?
Was this page helpful?