CSharp/C# Tutorial - C# ToString






The ToString method returns the default textual representation of a type instance.

This method is overridden by all built-in types.

Example

Here is an example of using the int type's ToString method:

int x = 1; 
string s = x.ToString(); // s is "1" 

You can override the ToString method on custom types as follows:

public class Person { 
    public string Name; 
    public override string ToString() { return Name; } 
} 
Person p = new Person { Name = "CSS" }; 
Console.WriteLine (p); // CSS 

If you don't override ToString, the method returns the type name.