Override ToString()

Somewhere - forgot where - I read a note that if you override ToString, you should always include the default "g" formatter.
Is this true? And if so, would this be a correct implementation:
        public override string ToString()
            return ToString(null, null);

        public string ToString(string format)
            return ToString(format, null);

        public string ToString(IFormatProvider provider)
            return ToString(null, provider);

        public string ToString(string format, IFormatProvider provider)
            // Default format, which always is "g".
            string defaultFormat = "g";
            // Our custom default format.  // example
            string customFormat = "{0}-NX{1:00}";
            string toString = String.Empty;

            if (format == null) format = defaultFormat;
            if (format == defaultFormat) format = customFormat;
            if (provider == null) provider = _cultureInfo;
            toString = string.Format(provider, format, _field1, _field2);

            return toString;

Open in new window

Also, what is the common name or label of this "g"? A formatter? Default format? Or?

LVL 56
Gustav BrockCIOAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
When you say "override ToString," are you strictly talking about when you do so with a IFormatProvider, or are you talking about any time you override ToString?
Fernando SotoRetiredCommented:
Hi : Gustav;

I do not remember ever reading something like that. Please have a read of the following two links.

Object.ToString Method

How to: Override the ToString Method (C# Programming Guide)
Gustav BrockCIOAuthor Commented:
I'm thinking of a class from which you normally would return a set of values and you have a default way to present these, say cars, with brand and model:

    Brand: Ford
    Model: Taurus

    Brand: Fiat
    Model: Punto

Neither Brand, nor Model makes sense on its own if you wish to use ToString to identify an object. Thus, to have the simplest possible way to do this, I would override ToString() to return:

    string.Format("{0} {1}", brand, model);

with resulting string like:

    Ford Taurus
    Fiat Punto

As I understand it, ToString("g") should return the exact same string, while a custom format could be ToString("Your new car is a {0} model {1}.")

I have no idea from where this "g" originates, but I can see it is accepted. For example:

    double d = 123.56;

returns the localized format: 123,56

In my code example, IFormatProvider is accepted though ignored but, of course, it could be implemented if necessary.

Thanks Fernando, I was not aware of the new options for Windows 8.1. However, my question does not relate to Win8.1 specifically - I'm looking for a general view.

käµfm³d 👽Commented:
According to this article:

...your assumptions are correct. The article suggests that your overridden ToString() (no params) should internally call the ToString version that takes the specifier, specifying "g" as the format. The "g" format should return whatever you would expect the "default" view of the string to be.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gustav BrockCIOAuthor Commented:
Ah, there it was. I just browsed the article and will give it a closer look later as it seems to cover it all.

Have a nice weekend both of you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.