2

Here's what I'm trying to get at: System.String is a class that contains an array of characters and if we were to make a string and then print it out:

String text = "Hello world!";
Console.WriteLine(text);

the output would be Hello World!

Now lets say I made my own class:

public class Output 
{
    string aProperty;
    int Count;
    ...
}

and if I were to create a new object of that type and try to write it out:

Output output = new Output();
Console.WriteLine(output);

I would get this back:

Namespace.Output

How could I change this so it always prints the content of Output.aProperty instead of the default text?

halfer
  • 19,824
  • 17
  • 99
  • 186
DethoRhyne
  • 930
  • 1
  • 9
  • 29
  • 2
    override ToString in class Output –  Jun 30 '16 at 07:40
  • 1
    you can ovveride the tostring method and write your own implementation – lordkain Jun 30 '16 at 07:41
  • detailed explanation that is what you get @ SO, here is a well explained answer for your question http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – Mehavel Jun 30 '16 at 07:44
  • Hi there. I don't have enough knowledge to determine if this is a duplicate of another question, but in general this kind of commentary is not ideal in posts themselves, IMO. Feel free to put it in the comments if you like, or post to _Meta_ about it. – halfer Sep 16 '16 at 19:00

5 Answers5

4

WriteLine method simply writes what the ToString of the object returns. ToString is defined (as virtual) in Object class, so you need to override it to have a "custom" output:

public class Output 
{
    string aProperty;
    int Count;

    public override string ToString()
    {
        return string.Format("{0}: {1}", aProperty, Count);//Or whatever you want
    }
}
Matteo Umili
  • 3,412
  • 1
  • 19
  • 31
2

You simply need to overwrite the ToString() method (a virtual method of object):

public class Output 
{
    string aProperty;
    int Count;

    public override string ToString()
    {
        return "I am an Output with aProperty: " + aProperty + " and Count " + Count;
    }
}

Or in better C# 6:

public class Output 
{
    string aProperty;
    int Count;

    public override string ToString() => $"I am an Output with aProperty: {aProperty} and Count {Count}";
}
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Bit of topic question, does the `=>` ¿operator? have a name? I don't know about it and i have seen it in a lot of places so I want to learn about it, but googling "That arrow from .net" is not the best... – Aimnox Jun 30 '16 at 07:52
  • @Aimnox In this context `=>` creates an _Expression Bodied Function_. You can read more [here](https://msdn.microsoft.com/en-us/magazine/dn802602.aspx) (scroll down to "Expression Bodied Functions and Properties") – René Vogt Jun 30 '16 at 07:55
2

Override the ToString method

public class Output 
{
    string aProperty;
    int Count;
    ...

    public override string ToString()
    {
        return aProperty;
    }
}
ArgusMagnus
  • 679
  • 5
  • 14
2
public override string ToString()
{
    return aProperty;
}

this technique is also very usefull when you use the debugger

lordkain
  • 3,061
  • 1
  • 13
  • 18
2

What you do there is call the ToString() method. All clases have a ToString. By default they have the one they take from the object class, and that is the namespace.

So you just need to override the ToString of your class:

public class Output 
{
    string aProperty;
    int Count;
    ...
    public override string ToString()
    {
        return aProperty; //Or whatever you want
    }
}

This will also modify what you see if you put an Ouput object on a listbox, combobox, etc

Aimnox
  • 895
  • 7
  • 20
  • 1
    Thanks for the hint at the end about listboxes. I've had situations where I had to do some bindings and this is exactly what I needed to know! However, regarding the question, I will mark Matteo's as the answer because he answered first. I hope you don't mind! – DethoRhyne Jun 30 '16 at 07:46
  • No, I dont mind at all. It's just some points on a website. Glad I could help. It's really usefull in cases where you have to select a person (for example) you can just load a combo with a buncho of your custom `person` object, and then you can take the whole object with SelectedItem. No need for loading the names, then search for the object with that name, etc – Aimnox Jun 30 '16 at 07:49
  • Very true indeed. one of my apps is bills management and with that simple selectedItem I can have the exact bill, that was binded to a datatemplate, I need for further processing! :) – DethoRhyne Jun 30 '16 at 07:55