I have searched both Google and this site to find an 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.
By overriding the toString( ) method, we are customizing the string representation of the object rather than just printing the default implementation. We can get our desired output depending on the implementation, and the object values can be returned.
The toString method is used to return a string representation of an object. If any object is printed, the toString() method is internally invoked by the java compiler. Else, the user implemented or overridden toString() method is called.
Object toString() Method in Java. Assigning values to static final variables in Java. Covariant Return Types in Java. Object Class in Java.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With