If you’ve ever used string.format with more than five placeholders, you’ll probably be able to relate to the pain.

One: Painful experiences

First of all, I have written a section of code, everyone to experience:

Loghelper.writelog (string.Format(@"=== Square Sending service === = [Step 4] Leaflet Sending is successful. MarketID = {0}, marketName = {1}, a total of customers: {2}, success: {3}, failure: {4}, repeat: {5}, current priority: shopID = {7} {6}, belong to ", leafletEntity. MarketingID, leafletEntity.MarketingName, leafletEntity.CustomerList.Count,leafletEntity.SuccessCount, leafletEntity.FailCount, leafletEntity.RepeatCustomerNum, leafletEntity.Priority, leafletEntity.ShopID));Copy the code

There are up to eight placeholders here, and when I wrote this, there were three pain points.

1. Placeholders should not be written wrong

Like here {0},{1},{2}…. Can’t write {0},{0},{2}, resulting in duplicate output results.

2. The number of parameter values cannot be more or less

For example, the final leafletEntity.ShopID parameter value is lost, resulting in 8 placeholders and 7 parameters, which will cause an exception.

3. The sequence of parameter values must be correct

Number of parameter values, it is difficult to ensure the order is not wrong, such as leafletEntity here. The FailCount and leafletEntity RepeatCustomerNum, Upside down for leafletEntity. RepeatCustomerNum and leafletEntity. FailCount, you can see it???????

Two: solutions

String. Format is also a relic of the C language’s past. Now C#6.0 has an interpolated string that perfectly solves all three problems.

Loghelper. WriteLog($@"=== Square sending service === = [Step 4] Leaflet sending succeeds. marketID={leafletEntity.MarketingID}, marketName={leafletEntity.MarketingName}, Total customer: {leafletEntity. CustomerList. Count}, success: {leafletEntity. SuccessCount}, failure: {leafletEntity. FailCount}, Repeat: {leafletEntity. RepeatCustomerNum}, current Priority: {leafletEntity. Priority}, subordinate shopID = {leafletEntity. ShopID} ");Copy the code

Interpolated strings are just strings with “$” in front of them, so let’s take a moment and see if we can solve the three pain points I mentioned earlier.

1. The use of ternary operators in interpolation

Many times in business development, you can’t just populate a variable, and it’s best to support some expressive value, such as the most commonly used triplet operator

<1> Incorrect usage

If you just write it like this, it won’t pass the nasty compiler, like this.

<2> Correct usage

The solution can be as simple as adding a () to the periphery.

2. Use of interpolation in complex logic

If you have complex business logic, suggest a separate method.

class Program { static void Main(string[] args) { var num = 10; var info = $"i={GetNum(num)}"; } public static int GetNum(int num) { return num == 10 ? 1:2; }}Copy the code

Your business logic is relatively simple and can be implemented using inline delegates.

Three: Explore the principles

To explore what the interpolated string syntax sugar is at the IL level, you can use the ILSpy tool to see the IL code.

As you can see from the screenshot above, the so-called “interpolated string” is actually string.format, the compiler’s syntax sugar

Note:

Since there is a box operation, you need to be careful. If Console.WriteLine is executed multiple times, there is a performance penalty.

static void Main(string[] args) { int i = 1, j = 2, k = 3; var it = i.ToString(); var jt = j.ToString(); var kt = k.ToString(); for (int m = 0; m < int.MaxValue; m++) { Console.WriteLine($"i={it},j={jt},k={kt}"); }}Copy the code

Well, that’s all for this article. I hope it helps you.