Pages

Thursday, June 26, 2008

Override ToString method

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);
}

}

No comments: