Home > Backend Development > C++ > What Does the `$` Prefix Mean in C# String Interpolation?

What Does the `$` Prefix Mean in C# String Interpolation?

Barbara Streisand
Release: 2025-01-03 03:12:42
Original
246 people have browsed it

What Does the `$` Prefix Mean in C# String Interpolation?

What is the Significance of the $ Prefix in C# String Interpolation?

In C#, the $ symbol preceding a string (like "$"text") is a shorthand notation for String.Format, which relates to string interpolations, a new feature introduced in C# 6.

Usage in String Interpolation

Typically, the $ prefix is used to incorporate other values into strings. Prior to C# 6, this required using String.Format as shown below:

var anInt = 1;
var aBool = true;
var aString = "3";
var formated = string.Format("{0},{1},{2}", anInt, aBool, aString);
Copy after login

With string interpolation, this becomes much simpler:

var anInt = 1;
var aBool = true;
var aString = "3";
var formated = $"{anInt},{aBool},{aString}";
Copy after login

Alternative Format Using $@

An alternative form of string interpolation that blends the capabilities of $"" and @"" is $@. This allows for the use of string interpolations within verbatim strings without requiring the use of throughout your string.

For example, the following lines:

var someDir = "a";
Console.WriteLine($@"c:\{someDir}\b\c");
Copy after login

will output:

c:\a\b\c
Copy after login

The above is the detailed content of What Does the `$` Prefix Mean in C# String Interpolation?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template