Code inspection: Formatting is specified, but interpolated string expression is not IFormattable
This inspection reports usages of the format string in an interpolation expression, where the interpolated expression does not implement the IFormattable
and thus does not support custom formatting.
In the example below, the X4
format string will not be applied to number
, because it is of type string
, which does not IFormattable
.
One of the ways to solve this problem, would be to convert the original string into a type that implements IFormattable
— for example, int
or double
— so that the desired format string can be applied to it.
string ConvertToUnicodeValue(string number)
{
return $"U+{number:X4}";
}
string ConvertToUnicodeValue(string number)
{
int value = int.Parse(number);
return $"U+{value:X4}";
}
Last modified: 30 September 2024