I want to show custom ToString() formats in intellisense like DateTime.ToString()
below.
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";
}
}
}
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";
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With