public class Program
{
public void Main()
{
IFormattable string1 = $"{1},{"test"}"; //Works
FormattableString string2 = $"{1},{"test"}"; //Works
LocStr string3 = $"{1},{"test"}"; ///Error: Cannot implicitly convert type 'string' to 'LocStr'
}
public class LocStr : IFormattable
{
public string ToString(string format, IFormatProvider formatProvider)
{
throw new NotImplementedException();
}
}
}
I am trying to create my own custon FormattableString class, however whether my class inherits from IFormattable or FormattableString, it cannot be directly be assigned from a formated string, as in LocStr testStr= $"Hello {User.Name}";
I tried inheriting from IFormattable, FormattableString, neither worked.
I checked if I can override assignment operator = but it is apparently not allowed in C#
as in public static LocStr operator =(string a)
but it is not allowed.
There is some compiler magic going behind the scenes. If you define variable type as explicitly
string
or allow compiler to determine it withvar
then it will result in typestring
:But for formattable strings there is also a special type
FormattableString
:which you can actually use in your code which results in the following behavior:
Demo @sharplab.io (also check out the decompilation to C#).
This is actually described in Implicit conversions and how to specify
IFormatProvider
implementation section of the docs:Compiler out of the box does not know how to convert the interpolated string into your specific type but since C# 10 you can implement the interpolated string handler pattern (not that you should though in general case, usually it is needed for specific high performance scenarios):
Updated demo @sharplab.io