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.
source share