The Cosmic C# String Theory
All Objects is C# are derived from System.Object. System.Object has “Cosmic” Universal Methods:
GetType () – This Method uses Reflection to “Reflect” on the Object to determine its C# Type. For two objects that have identical runtime types, x and y, the following example of the ReferenceEquals (Object left, Object right) Method Object.ReferenceEquals (x.GetType (), y.GetType()) returns true.
ToString () – This Method allows all C# Software Entities to have a suitable informational display representation. ToString () is the major formatting method in the .NET Framework. It converts an Object to its “String Representation”
GetHashCode () – Gets a numeric value that is used to identify an object during equality testing. It can also serve as an index for an object in a collection. The GetHashCode () method is suitable for use in hashing algorithms and data structures such as a hash table
You can override the .ToString() method to get useful information from complex Types that simply return the string display of the Type. This is generally not very useful in your programs.
The Overridden C# ToString() Method
You override the toString method to product an output for the Consuming Software Entity to use for populating controls and other useful purposes like Intellisense debugging.
Here is an example of a State DTO Object ToString() result without an Overload:
|
1 |
<span style="color: #800000;">var contact = new Contact</span> |
|
1 |
<span style="color: #800000;">{</span> |
|
1 |
<span style="color: #800000;">Address = "123 Main",</span> |
|
1 |
<span style="color: #800000;">City = "AnyTown",</span> |
|
1 |
<span style="color: #800000;">Name = "Jim Smith",</span> |
|
1 |
<span style="color: #800000;">State = "AnyState",</span> |
|
1 |
<span style="color: #800000;">Type = "Sales",</span> |
|
1 |
<span style="color: #800000;">Zip = "12345"</span> |
|
1 |
<span style="color: #800000;">};</span> |
var dtoStringInfo = contact.ToString();
Resulting Display for CustomerContactsDtoRequest: TCMS.Models.CustomerContacts.Aggregates.Contact
It simply returns the Run-time Type
Here is an example of the same State DTO Object .ToString() result with this Overload:
public override string ToString()
{
return string.Format(“{0} is a {1} contact with an address of {2} in {3}, {4} with a zip code of {5}”,
Name, Type, Address, City, State, Zip);
}
Displayed Value:
Jim Smith is a Sales contact with an address of 123 Main in AnyTown, AnyState with a zip code of 12345
It Returns a Usable Representation of the Class Properties
When in Debug Mode a simple hover over the variable will reveal all of the values for validation without having to drill down into the object.
When used at Run-time the object provides a fully functional display of the State Information for Application usage without Additional Process Formatting.
IFormattable and the IFormatProvider Interfaces
… Sometimes you need more than just the simple overload demonstrated above
Bill Wagner’s 50 Specific Ways to Improve Your C# in a PDF Download:
Item 5 is a Detailed Analysis of C# ToString() Overrides!
The following two tabs change content below.
I am a Principal Architect at Liquid Hub in the Philadelphia area specializing in Agile Practices as a Certified Scrum Master (CSM). I use Test Driven Development (TDD) and Acceptance Test Driven Development (ATDD) with Behavior Driven Development (BDD) as my bridge to Agile User Stories Acceptance Criteria in a Domain Driven Design (DDD) implementing true RESTful services
Latest posts by Brad Huett (see all)
- DevOps: A Bridge to Your DevOps Culture - March 25, 2016
- Embracing Test Driven Development (TDD) - March 25, 2016
- DevOps: Delivering Agile Projects - March 25, 2016


