C # 6.0:string interpolation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.