Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show custom ToString() formats in intellisense?

I want to show custom ToString() formats in intellisense like DateTime.ToString() below.

enter image description here

Below is sample code where I would like to show "a" or "b" in intellisense when someone types myObject.ToString("").

public class MyClass : IFormattable
{
    public string ToString(string format, IFormatProvider formatProvider)
    {
        switch (format)
        {
            case "a":
                return "A";
            case "b":
                return "B";
            default:
                return "A";
        }
    }
}
like image 677
imlokesh Avatar asked Aug 30 '25 17:08

imlokesh


1 Answers

You can use XML Doc Comments for this.

For example, for your ToString() something along these lines:

public class MyClass : IFormattable
{
    /// <summary>Converts this to a formatted string.</summary>
    /// <param name="format">
    ///   A format string. This may have the following values:
    ///   <list type="table">
    ///     <listheader>
    ///       <term>Format strings</term>
    ///     </listheader>
    ///     <item>
    ///       <term>"a"</term>
    ///       <description>Format using "a"</description>
    ///     </item>
    ///     <item>
    ///       <term>"b"</term>
    ///       <description>Format using "b"</description>
    ///     </item>
    ///   </list>
    /// </param>
    /// <param name="formatProvider">A format provider.</param>
    /// <returns>The formatted string.</returns>

    public string ToString(string format, IFormatProvider formatProvider)
    {
        switch (format)
        {
            case "a":
                return "A";
            case "b":
                return "B";
            default:
                return "A";
        }
    }
}
like image 65
Matthew Watson Avatar answered Sep 02 '25 08:09

Matthew Watson