Search code examples
c#tostringconventions

Conventions for ToString


What are the conventions for ToString() overrides? The class in question has both a Name property, but also an Id.


Solution

  • You can refer to following links

    Overriding System.Object.ToString() and Implementing IFormattable

    Demystifying C# Programming's ToString Method

    How To: Override the ToString Method

    My Rule of thumb

    1. It should be in sync with Equals and GetHashCode
    2. It should be in sync with Parse method (If I am providing)
    3. Use IFormattable if custom formatting is needed