Search...

Monday, June 4, 2012

Override ToString Method in C#

using System;

namespace OverrideToString
{
class Program
{
static void Main(string[] args)
{
Person _person=new Person("Kaushik","Mistry","Delhi","8800xxxxxx");
Console.Write(_person.ToString());
Console.Read();
}
}

class Person
{
public Person(String firstName,String lastName,String address,String phoneNumber)
{
FirstName=firstName;
LastName=lastName;
Address=address;
PhoneNumber=phoneNumber;
}

public String FirstName { get; set; }
public String LastName { get; set; }
public String Address { get; set; }
public String PhoneNumber { get; set; }

public override string ToString()
{
return String.Format("First Name\t:\t{0}\nLast Name\t:\t{1}\nAddress\t\t:\t{2}\nPhone Number\t:\t{3}",
FirstName,
LastName,
Address,
PhoneNumber);
}
}
}

********************************************************************************



No comments: