MorkaLork Development

Interesting stuff I've picked up over the years...

Override .ToString()

2009-04-17 07:21:42 | 341 views | csharp class method tutorial override tostring object namespace

This is just to show how the Object method ToString can be overriden:

If you have a class with some properties, say class Person, with the properties Name, Age and Gender, you cannot simply use Person p = new Person(), p.ToString() sinse all you will get is, by syntax, Namespace.Class.
If you have a project with Namespace MyFirstApplication and a class called Person, then the following code:


Person p = new Person();
Console.WriteLine(p.ToString());

will output:

MyFirstApplication.Person

That's no good. What we get is a description of the object at hand, in this case Person. Now, let's say that this class has two properties, Name, Age and Gender, and that we just want that when we use ToString(). There is no point to this except showing that it's possible.

This is how the class could look:


public class Person
{
private string name;
private int age;
private string gender;

public string Name
{
get{return name;}
set{name = value;}
}
public int Age
{
get{return age;}
set{age = value;}
}
public string Gender
{
get{return gender;}
set{gender = value;}
}
}


Now, if we add an override to the ToString() method that is automatically inherited to the class, it would look like this:


public override string ToString()
{
return Name + ", " + Age + ", " + gender;
}


System.Object.ToString() is a virtual method and can as such be overridden. Remove the line return base.ToString() and enter the line as shown in the code above. ToString() will now return a string with Name, Age and Gender, separated with a comma.

Use it like this:


public static void Main(string[] args)
{
Person p = new Person();
p.Name = "Jay Letterman";
p.Age = 93;
p.Gender = "Unknown";

Console.WriteLine(p.ToString());
Console.Read();
}


Now you will get the following output:

Jay Letterman, 93, Unknown

WTF!?



Allright, the point of this is that it can be used when getting information from an object is hard. The above example serves no purpose since you could just as well create a method that does the same, but I will now show you an example when it might be handy to use this technique:

We want to create a generic list based on a class, Person. To output this, we can do it the long way, or the short way. This code will show both ways:

Person.cs




namespace TestApp
{
public class Person
{
public string Name;
public int Age;
public string Gender;

public Person(string name, int age, string gender)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
}

public override string ToString()
{
return Name + ", " + Age + ", " + Gender;
}

}
}


Program.cs




using System;
using System.Collections.Generic;

namespace TestApp
{

class Program
{
public static void Main(string[] args)
{
//Create a list
List<Person> person = new List<Person>();

//Add items to the list
person.Add(new Person("Joe Handyman", 53, "Male"));
person.Add(new Person("Cindy Carpenter", 42, "Femal"));
person.Add(new Person("Kang", 143, "Unknown"));

//The LOOOONG way
foreach(Person p in person)
{
Console.WriteLine("{0}, {1}, {2}", p.Name, p.Age, p.Gender);
}

Console.WriteLine("");

//The short way
foreach(Person p in person)
{
Console.WriteLine(p.ToString());
}

Console.Read();
}
}
}


The output will look like this:
imagehttp://admin.morkalork.com/uploads/images/csharp/OverrideTostring1.png

You get the same output both ways. This can be handy if you're outputting entire lists to file or something.


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.