ToString

suggest change

ToString() is defined on object, so every type has it, including value types. The implementation on object returns the type name. The fragment below will print out “User” to the console.

public class User
{
    public string Name { get; set; }
    public int Id { get; set; }
}

...

var user = new User {Name = "User1", Id = 5};
Console.WriteLine(user.ToString());

However, the class User can also override ToString() in order to alter the string it returns. The code fragment below prints out “Id: 5, Name: User1” to the console.

public class User
{
    public string Name { get; set; }
    public int Id { get; set; }
    public override string ToString()
    {
        return string.Format("Id: {0}, Name: {1}", Id, Name);
    }
}

...

var user = new User {Name = "User1", Id = 5};
Console.WriteLine(user.ToString());

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents