How to use string interpolation in C # 9

0

String interpolation is a technique that allows you to insert the value of an expression into a literal string. This is also known as variable substitution, variable interpolation, or variable expansion. This is the process of evaluating a string literal that contains one or more placeholders that will be replaced with the corresponding values.

String interpolation is supported in many programming languages ​​including Python, Perl, PHP, Ruby, Java, Scala, and more. Introduced in C # from C # 6. This article describes how to use string interpolation in C #.

Visual Studio 2019 must be installed on your system to use the code samples provided in this article. If you don’t have a copy yet Download Visual Studio 2019 here..

Create a Console Application Project in Visual Studio

First, let’s create a .NET Core Console Application project in Visual Studio. Assuming you have Visual Studio 2019 installed on your system, follow the steps outlined below to create a new .NET Core Console Application project in Visual Studio.

  1. Launch the Visual Studio IDE.
  2. Click on Create a new project.
  3. In the Create New Project window, select Console Application (.NET Core) from the list of templates that appears.
  4. [次へ]Click on.
  5. Indicated below[新しいプロジェクトの構成]In the window, enter the name and location of the new project.
  6. [作成]Click on.

The following sections of this article will use this console application project to handle string interpolation.

Structure of interpolated strings in C #

In programming, the interpolated string is a literal string that contains an interpolated expression. Whenever the interpolated string resolves to a result string, the interpolated expression is replaced with the string representation of the result of the expression.

The interpolated string is $ symbol, and there can be no space between $ Character and string literals. The following statement shows the syntax of the interpolated expression.

{[,][:]}

Note that the items inside the brackets are optional. NOT. interpolationExpression element represents an expression that produces a result or an output string. NOT. alignment element represents a constant expression whose value specifies the minimum number of characters that must be included in the string representation of the result of the expression when the expression is evaluated. If the value is positive, the string representation is right justified. If the value is negative, the string representation is left justified. NOT. formatString The element, as its name suggests, represents the result of the expression, a format string suitable for the object to be formatted.

Create an interpolated string in C #

If you are using string interpolation, you must first insert the $ character just before the string. You can then embed these arguments directly into the interpolated string, rather than providing the parameters of the format elements individually.

The interpolation formula is contained in the open / closed brace. ({ When }).. The following code snippet demonstrates how to use string interpolation to replace string literals with text.

var name = "Joydip";
Console.WriteLine($"Hello, {name}");

Then consider the following code snippet.

string productName = "Lenovo Legion Laptop";
int counter = 5;
string output = string.Format("Today, {0} has been sold {1} times.",
                                               productName, counter);

When you run the program, the contents of a named string object output At run time it looks like this:

Today, Lenovo Legion Laptop has been sold 5 times.

The above code can also be written:

string productName = "Lenovo Legion Laptop";
int counter = 5;           
string output = $"Today, {productName}, has been sold {counter} times.";

Interpolated string must be a compiler string literal

The interpolated string looks like a simple way to create a string pattern that can evaluate the expression. However, it’s important to understand that string interpolation in C # is just syntactic sugar generated by the compiler to dynamically create strings. Note that the format string must be a static string literal. That is, the interpolated string must be available as a string literal at compile time.

Use special characters in C # interpolated strings

Next, let’s see how to use special characters in the interpolated string. Consider the following example to illustrate this.

string productName = “Lenovo Legion laptop”;
Double price = 1600.00;
Console.WriteLine ($ “Customer requested” How much is {productName}? “” “);
Console.WriteLine ($ “The seller replied that the price for {productName} is $ {price}.”);

As you can see, including special characters is as easy as escaping special characters with a backslash () ).

You can also use string interpolation to insert values ​​into a string and control the text format of the resulting string.Interpolated string of type string Usually converted String.Format Method call. Conversely, if the interpolated string is a type IFormattable Also FormattableString, The compiler calls FormattableStringFactory.Create Method.

Using string interpolation may result in a slight decrease in performance. However, recent versions of .NET have been significantly optimized, and the flexibility offered by string interpolation generally outweighs the cost of performance.

Copyright © 2021 IDG Communications, Inc.


Source link

Share.

About Author

Leave A Reply