In this article

The $ special character identifies a string literal as an interpolated string. An interpolated string is a string literal that might contain interpolation expressions. When an interpolated string is resolved lớn a result string, items with interpolation expressions are replaced by the string representations of the expression results. This feature is available starting with C# 6.

Bạn đang xem: Chuỗi f&b là gì

String interpolation provides a more readable và convenient syntax lớn create formatted strings than a string composite formatting feature. The following example uses both features to lớn produce the same output:

string name = "Mark";var date = DateTime.Now;// Composite formatting:Console.WriteLine("Hello, 0! Today is 1, it"s 2:HH:mm now.", name, date.DayOfWeek, date);// String interpolation:Console.WriteLine($"Hello, name! Today is date.DayOfWeek, it"s date:HH:mm now.");// Both calls produce the same output that is similar to:// Hello, Mark! Today is Wednesday, it"s 19:40 now.

Structure of an interpolated string

To identify a string literal as an interpolated string, prepover it with the $ symbol. You cannot have sầu any white space between the $ & the " that starts a string literal.

The structure of an item with an interpolation expression is as follows:

<,><:>Elements in square brackets are optional. The following table describes each element:

interpolationExpressionThe expression that produces a result khổng lồ be formatted. String representation of null is String.Empty.
alignmentThe constant expression whose value defines the minimum number of characters in the string representation of the expression result. If positive sầu, the string representation is right-aligned; if negative sầu, it"s left-aligned. For more information, see Alignment Component.
formatStringA format string that is supported by the type of the expression result. For more information, see Format String Component.

The following example uses optional formatting components described above:

Console.WriteLine($"|"Left",-7|"Right",7|");const int FieldWidthRightAligned = 20;Console.WriteLine($"Math.PI,FieldWidthRightAligned - default formatting of the pi number");Console.WriteLine($"Math.PI,FieldWidthRightAligned:F3 - display only three decimal digits of the pi number");// Expected output is:// |Left | Right|// 3.14159265358979 - default formatting of the pi number// 3.142 - display only three decimal digits of the pi numberBeginning with C# 10, you can use string interpolation to initialize a constant string when all the expressions used for placeholders are also constant strings. In other words, every interpolationexpression must be a string, and it must be a compile time constant.

Special characters

To include a brace, "" or "", in the text produced by an interpolated string, use two braces, "" or "". For more information, see Escaping Braces.

Xem thêm: Acrylates Copolymer Là Gì ? Có Công Dụng Gì Trong Mỹ Phẩm? Acrylate Copolymer

As the colon (":") has special meaning in an interpolation expression thành phầm, in order lớn use a conditional operator in an interpolation expression, encđại bại that expression in parentheses.

The following example shows how to lớn include a brace in a result string & how lớn use a conditional operator in an interpolation expression:

string name = "Horace";int age = 34;Console.WriteLine($"He asked, "Is your name name?", but didn"t wait for a reply :-{{");Console.WriteLine($"name is age year(age == 1 ? "" : "s") old.");// Expected output is:// He asked, "Is your name Horace?", but didn"t wait for a reply :-{// Horace is 34 years old.An interpolated verbatim string starts with the $ character followed by the
character. For more information about verbatlặng strings, see the string & verbatim identifier topics.

$"..." are valid interpolated verbatyên strings. In earlier C# versions, the $ token must appear before the

Implicit conversions & how to specify IFormatProvider implementation

There are three implicit conversions from an interpolated string:

Conversion of an interpolated string lớn a FormattableString instance that represents a composite format string along with the expression results to lớn be formatted. That allows you to lớn create multiple result strings with culture-specific nội dung from a single FormattableString instance. To vì that, call one of the following methods:

You can also use the ToString(IFormatProvider) method lớn provide a user-defined implementation of the IFormatProvider interface that supports custom formatting. For more information, see the Custom formatting with ICustomFormatter section of the Formatting types in .NET article.

The following example uses implicit conversion khổng lồ FormattableString to lớn create culture-specific result strings:

double speedOfLight = 299792.458;FormattableString message = $"The speed of light is speedOfLight:N3 km/s.";System.Globalization.CultureInfo.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("nl-NL");string messageInCurrentCulture = message.ToString();var specificCulture = System.Globalization.CultureInfo.GetCultureInfo("en-IN");string messageInSpecificCulture = message.ToString(specificCulture);string messageInInvariantCulture = FormattableString.Invariant(message);Console.WriteLine($"System.Globalization.CultureInfo.CurrentCulture,-10 messageInCurrentCulture");Console.WriteLine($"specificCulture,-10 messageInSpecificCulture");Console.WriteLine($""Invariant",-10 messageInInvariantCulture");// Expected output is:// nl-NL The tốc độ of light is 299.792,458 km/s.// en-IN The tốc độ of light is 2,99,792.458 km/s.// Invariant The speed of light is 299,792.458 km/s.

Additional resources

If you are new khổng lồ string interpolation, see the String interpolation in C# interactive sầu tutorial. You can also kiểm tra another String interpolation in C# tutorial that demonstrates how lớn use interpolated strings to lớn produce formatted strings.

Compilation of interpolated strings

If an interpolated string has the type string, it"s typically transformed into a String.Format method call. The compiler may replace String.Format with String.Concat if the analyzed behavior would be equivalent to lớn concatenation.

If an interpolated string has the type IFormattable or FormattableString, the compiler generates a Điện thoại tư vấn lớn the FormattableStringFactory.Create method.

Xem thêm: Resonant Là Gì - Resonant Nghĩa Là Gì Trong Tiếng Việt

C# language specification

For more information, see the Interpolated strings section of the C# language specification.