Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Intermediate C# System.Object Object.ToString

Maddalena Menolascina
Maddalena Menolascina
6,668 Points

Object.ToString example

Hi all, I apologize for being so silly on this method, but although I understand its purpose I can't make sense out of it.

I understand that we use it to convert an object into a displayable string and in order to avoid that it returns its fully qualified name we override it.

But still, when I work on my own projects I can't use it at all, because it slips out of my mind before I know XD.

Can anybody help me with a simple example ( no msdn's ones please... I have been staring at their explanation speechless for hours)

Thank you guys

may you post a sample of your code

Maddalena Menolascina
Maddalena Menolascina
6,668 Points

Hi Fanuel,

I don't need it in my code (I am working on a very simple project that doesn't need any of that), in fact the reason why I am asking an example is so that I can understand it better and use it in future projects where I may need it. If I had another practical example I could make this notion my own.

However in the following exercise I succeeded without fully understanding the method properly.

https://teamtreehouse.com/library/intermediate-c/systemobject/objecttostring

Sorry, I hope that this makes sense. But thank you for your quick answer

2 Answers

Steven Parker
Steven Parker
231,268 Points

:point_right: Here's a hopefully more understandable example.

Suppose we have a class named colors that represents primary colors internally as a number, but we want to be able to have the name of the color show just by treating the class as a string:

public class colors
{
    public int MyColor;

    public override string ToString()
    {
        switch MyColor
        {
            case 1: return "red";
            case 2: return "green";
            case 3: return "blue";
        }
        return "something else";
    }
}

So then, later, you could have something like this:

var x = new colors();
x.MyColor = 1;
Console.WriteLine("The chosen color is " + x);

And it would print: "The chosen color is red". Does that help?

Kyle Jensen
Kyle Jensen
Courses Plus Student 6,293 Points

Good example, but it led me to another question. In the course example, the teacher was able to use the class to make checks(all the methods checking points that were created earlier that didn't receive any changes after ToString() was overridden), but when he logged any results it prints the string. If it treats the class as a string, then how can it check integer values? Or, does it only use the ToString() override when printing them out? With it overridden, is this still valid if x is in a class that's now being treated as a string?

if(point.X == 5) { // the member is an integer even though it's in a class being treated as a string
    //do something..
}

// Or, do you now have to do one of the following:
//option 1
if(int.Parse(point.X) == 5) { //parse the member before checking
    //do something
}

//option 2
if(point.X == "5") { //check for string value of 5 instead of integer value
    //do something
}
Tom Maher
Tom Maher
1,529 Points

Following up on that last chunk of Steven's code, I'm slightly confused as to why the following code:

Console.WriteLine("The chosen color is " + x);

Prints 'The chosen color is red' the screen, whereas the following code:

Console.WriteLine("The chosen color is " + x.MyColor);

Prints 'The chosen color is 1' to the screen.

Is this because we've overridden the class itself, contingent on a particular property (e.g. we need to call the class instance itself for our overridden ToString() to work, and then it checks the properties on that class instance to determine the proper output)?

Maddalena Menolascina
Maddalena Menolascina
6,668 Points

Thank you Steven, it's just what I needed :)