Override ToString method

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
C# Code Snippets

C# Source Code Bank

See also …
edit

This C# code snippet overrides the ToString method inherited from System.Object in order to display the private variables.

public class MyClass
{
   private string customer ="";
   private int customerID = 0;
 
   public string Customer
   {
      get { return customer; }
      set { customer = value; }
   }
 
   public int CustomerID
   {
      get { return customerID; }
      set { customerID = value; }
   }
 
   public override string ToString()
   {
      return string.Format ("Customer = {0} ID = {1}", Customer, CustomerID);
   }
 
}

Personal tools