Hi,
I have noticed that the "Convert to string interpolation" does not take escaped curly brackets into account. See the following code sample:
C#using System;
namespace InterpolationTest
{
internal class Program
{
private static void Main(string[] args)
{
StringFormatTest();
CodeRushInterpolationTest();
CorrectInterpolationTest();
Console.ReadKey();
}
private static void StringFormatTest()
{
string value1 = "Rush";
string value2 = "Code";
string format = string.Format("{{0}}{0}", value1);
string result = string.Format(format, value2);
Console.WriteLine("String.Format: " + result);
}
private static void CodeRushInterpolationTest()
{
try
{
string value1 = "Rush";
string value2 = "Code";
string format = $"{{value1}}{value1}"; // this is 'string format = string.Format("{{0}}{0}", value1);' converted to string interpolation using CodeRush
string result = string.Format(format, value2);
Console.WriteLine("CodeRush interpolation: " + result);
}
catch (Exception ex)
{
Console.WriteLine("CodeRush interpolation error: " + ex.Message);
}
}
private static void CorrectInterpolationTest()
{
string value1 = "Rush";
string value2 = "Code";
string format = $"{{{0}}}{value1}";
string result = string.Format(format, value2);
Console.WriteLine("Correct interpolation: " + result);
}
}
}
The StringFormatTest function shows the original string.Format
The CodeRushInterpolationTest function shows the original string.Format converted to a string interpolation using CodeRush
The CorrectInterpolationTest function shows the correct conversion
The output from this program is:
String.Format: CodeRush
CodeRush interpolation error: Input string was not in a correct format.
Correct interpolation: CodeRush
Hi Dave,
Thank you for your feedback. I have reproduced this issue locally. We will fix it in our future builds.
Thanks for the speedy response
You are welcome!