CSharp - Type Creation ToString Method

Introduction

ToString() method returns the textual representation of a type instance.

This method is overridden by all built-in types.

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:

class Person
{
       public string Name;
       public override string ToString() => Name;
}

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

Demo

using System;
class MainClass// w ww .  j  av  a2  s  .c o  m
{
   public static void Main(string[] args)
   {

        Person p = new Person { Name = "Book2s.com" };
        Console.WriteLine (p);   

   }
}
class Person
{
       public string Name;
       public override string ToString() => Name;
}

Result