开发者

How is the ToString() method overridden and formatted within a class?

I have searched both Google and this site to find a开发者_C百科n answer. I have also read it in my class book, but I still don't understand how it is applied.

Here is what I know:

  • It is used to display what an object knows about itself.

  • It can be used to format the output about what it knows about itself.

  • It has be be overridden in the method to be used.


Thinking that you have a class like:

public class Person
{
   public string FirstName { get; set;}
   public string LastName { get; set;}
   private string age;
}

Using this:

public override string ToString()
{
   return string.Format("[First Name: {0}; Last Name: {1}; Age: {2}]", FirstName, LastName, age);
}

will reveal all the internals of the class. Actually it is best used for quickly retrieving public fields of the class in a pre-formatted manner (thus effectively converting the data encapsulated in the class into a string).

Also here is a very useful advice from the book Pro C# 2010 and the .NET 4 Platform: "Many classes (and structures) that you create can benefit from overriding ToString() in order to return a string textual representation of the type’s current state. This can be quite helpful for purposes of debugging (among other reasons). How you choose to construct this string is a matter of personal choice; however, a recommended approach is to separate each name/value pair with semicolons and wrap the entire string within square brackets. However, always remember that a proper ToString() override should also account for any data defined up the chain of inheritance. When you override ToString() for a class extending a custom base class, the first order of business is to obtain the ToString() value from your parent using the base keyword. Once you have obtained your parent’s string data, you can append the derived class’s custom information."


The base class for all .NET classes is the object type. The object class provides you with a default implementation of ToString() which by default just prints out the name of the class, unless the class has overridden the ToString() method to print something else. The name of the class is not particularly useful, so it's usually a good practice to override ToString() in any class you write, because it's useful to provide a human-readable representation of your class. The debugger will actually use your ToString() implementation by default when you want to inspect an instance of your class.

In your class, all you have to do is add this method:

public override string ToString()
{
    // return a string that has something to do with your class
}

Example:

public class Person
{
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public int Age {get;set;}

    public override void ToString()
    {
        // return whatever makes sense here...
        return string.Format("[Person: FirstName={0}, LastName={1}, Age={2}]", FirstName, LastName, Age);
    }
}

Note the string.Format method is just a way of creating a string. The first argument to string.Format is the "format," which can contain literal text as well as placeholders for values that you will provide as the other arguments. The {0}, {1}, and {2} above are placeholders for the FirstName, LastName, and Age arguments which are passed after the format. I would read up on string.Format if you want to know more.


Override ToString() method:

public class Test
{
   public string FirstName { get; set; }

   public string LastName { get; set; }

   public override string ToString()
   {
      // Do your formatting here
      return LastName + ", " + FirstName;
   }
}


You have to override ToString() in your class. Say I have a class with m_nDays, m_nHours, m_nMinutes... then I could override ToString() as follows:

public override string ToString()
{
    return string.Format("{0,5}d - {1:00}:{2:00}", m_nDays, m_nHours, m_nMinutes);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜