It is often necessary to format strings in the development process. We generally use the String.Format () method, which replaces each format item in the specified string with the text equivalent of the value of the corresponding object. Although this is common, it is sometimes confusing and error-prone. Because, we need to specify placeholders for each of the formatting items and correspond them to the actual object values. We also make sure that we assign a corresponding value to each placeholder. String interpolation in c#6.0 makes the string formatting much simpler. Using string interpolation, we no longer need to use placeholders and can reference our values directly.
Here is a simple example of using string interpolation.
stringValue1 ="Value 1";stringValue2 ="Value 2";//With String.FormatvarStringValue1 =string. Format ("{0}-{1}", Value1, Value2);//With String interpolationvarStringValue2 ="\{value1}-\{value2}";View Code
StringValue1 and StringValue2 return the same value in the code block above, but string interpolation makes the code look simpler and less prone to error. When you want to format a string with a large number of property values to display, you can simply put the corresponding value in the corresponding place, instead of using a placeholder.
C # 6.0:string interpolation