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

I searched both Google and this site to find the answer. I also read it in my study book, but I still don't understand how this is applied.

Here is what I know:

  • Used to display what an object knows about itself.

  • It can be used to format the output of what he knows about himself.

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

+6
source share
4 answers

Thinking 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 show all the internal elements of the class. In fact, it is best used to quickly retrieve the public fields of a class in a preformatted form (thus effectively converting the data encapsulated in a class into a string).

Also here is a very useful tip from Pro C # 2010 book and .NET 4 platform . "Many of the classes (and structures) you create can benefit from overriding ToString () to return a textual representation of the current status bar. This can be very useful for debugging purposes (among other reasons). As you decide to build this string, this is a matter of personal choice, but the recommended approach is to separate each / value name with semicolons and wrap the entire line in square brackets, however, always remember that a proper redefinition of ToString () should also take into account any data defined Inheritance Chain: When you override ToString () for a class extending a custom base class, the first order of business is to get the ToString () value from your parent using the base keyword. After you get the string data of your parents, you can add a custom derived class information.

+16
source

The base class for all .NET classes is the type object . The object class provides you with a default implementation of ToString() , which by default simply prints the class name if the class has not overridden the ToString() method to print something else. The class name is not particularly useful, so it is generally recommended to override ToString() in any class that you write, because it is useful for providing a human-readable representation of your class. The debugger actually uses the default ToString() implementation if you want to test 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 that the string.Format method is just a way to create a string. The first argument to string.Format is the "format", which can contain literal text, as well as placeholders for the values ​​that you specify as other arguments. {0} , {1} and {2} above are placeholders for the FirstName, LastName, and Age arguments that are passed after the format. I would read string.Format if you want to know more.

+2
source

Override the 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; } } 
+1
source

You must 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); } 
+1
source

Source: https://habr.com/ru/post/885877/


All Articles