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

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

Barbara Streisand
Barbara StreisandOriginal
2025-01-03 03:12:42263browse

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);

With string interpolation, this becomes much simpler:

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

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");

will output:

c:\a\b\c

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!

Statement:
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