我打算使用逐字字符串,但我错误地输入$而不是@。

但是编译器没有给我任何错误,并成功编译。

我想知道它是什么,有什么作用。我找过了,但什么也没找到。

然而,它不像一个逐字字符串,因为我不能写:

string str = $"text\";

在c#中字符串前面的$字符是什么意思?

string str = $"text";

我使用Visual studio 2015 CTP。


当前回答

很酷的功能。我只是想强调一下为什么这个比字符串好。格式,如果对某些人来说不明显。

我看到有人说order string。格式为“{0}{1}{2}”以匹配参数。你不被迫订购“{0}{1}{2}”在字符串。格式,也可以做“{2}{0}{1}”。然而,如果你有很多参数,比如20,你真的想要将字符串排序为“{0}{1}{2}…”{19}”。如果它是一团乱,你将很难排列你的参数。

使用$,您可以内联添加参数,而无需计算参数。这使得代码更容易阅读和维护。

$的缺点是,你不能很容易地在字符串中重复参数,你必须输入它。例如,如果您厌倦了输入System.Environment。换行符,你可以做string.format(“…{0}{0}…{0}", System.Environment.NewLine),但是,在$中,你必须重复它。你不能用$"{0}"将它传递给字符串。格式,因为$"{0}"返回"0"。

在旁注,我读了另一个重复主题的评论。我不能发表评论,所以,这就是。他说

string msg = n + " sheep, " + m + " chickens";

创建多个字符串对象。这其实是不对的。如果在单行中执行此操作,则它只创建一个字符串并放置在字符串缓存中。

1) string + string + string + string;
2) string.format()
3) stringBuilder.ToString()
4) $""

它们都返回一个字符串,并且只在缓存中创建一个值。

另一方面:

string+= string2;
string+= string2;
string+= string2;
string+= string2;

在缓存中创建4个不同的值,因为有4个“;”。

因此,像下面这样写代码会更容易,但你会创建5个插值字符串Carlos Muñoz corrected:

string msg = $"Hello this is {myName}, " +
  $"My phone number {myPhone}, " +
  $"My email {myEmail}, " +
  $"My address {myAddress}, and " +
  $"My preference {myPreference}.";

这将在缓存中创建一个字符串,而您有非常容易阅读的代码。我不确定性能,但是,我相信微软会优化它,如果还没有这样做。

其他回答

$是字符串的简写。格式和字符串插值一起使用,这是c# 6的一个新特性。正如在您的例子中使用的那样,它什么也不做,就像string.Format()一样。

当使用它来构建引用其他值的字符串时,它就发挥了作用。以前必须写成:

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

现在就变成:

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

还有一种不太为人所知的字符串插值形式,使用$@(两个符号的顺序很重要)。它允许@""字符串的特性与$""混合,以支持字符串插值,而不需要在整个字符串中使用\\。下面这两行:

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

将输出:

c:\a\b\c

我不知道它是如何工作的,但你也可以用它来标记你的值!

例子:

Console.WriteLine($"I can tab like {"this !", 5}.");

当然,您可以将“this !”替换为任何变量或任何有意义的内容,就像您也可以更改选项卡一样。

很酷的功能。我只是想强调一下为什么这个比字符串好。格式,如果对某些人来说不明显。

我看到有人说order string。格式为“{0}{1}{2}”以匹配参数。你不被迫订购“{0}{1}{2}”在字符串。格式,也可以做“{2}{0}{1}”。然而,如果你有很多参数,比如20,你真的想要将字符串排序为“{0}{1}{2}…”{19}”。如果它是一团乱,你将很难排列你的参数。

使用$,您可以内联添加参数,而无需计算参数。这使得代码更容易阅读和维护。

$的缺点是,你不能很容易地在字符串中重复参数,你必须输入它。例如,如果您厌倦了输入System.Environment。换行符,你可以做string.format(“…{0}{0}…{0}", System.Environment.NewLine),但是,在$中,你必须重复它。你不能用$"{0}"将它传递给字符串。格式,因为$"{0}"返回"0"。

在旁注,我读了另一个重复主题的评论。我不能发表评论,所以,这就是。他说

string msg = n + " sheep, " + m + " chickens";

创建多个字符串对象。这其实是不对的。如果在单行中执行此操作,则它只创建一个字符串并放置在字符串缓存中。

1) string + string + string + string;
2) string.format()
3) stringBuilder.ToString()
4) $""

它们都返回一个字符串,并且只在缓存中创建一个值。

另一方面:

string+= string2;
string+= string2;
string+= string2;
string+= string2;

在缓存中创建4个不同的值,因为有4个“;”。

因此,像下面这样写代码会更容易,但你会创建5个插值字符串Carlos Muñoz corrected:

string msg = $"Hello this is {myName}, " +
  $"My phone number {myPhone}, " +
  $"My email {myEmail}, " +
  $"My address {myAddress}, and " +
  $"My preference {myPreference}.";

这将在缓存中创建一个字符串,而您有非常容易阅读的代码。我不确定性能,但是,我相信微软会优化它,如果还没有这样做。

它创建了一个插值字符串。

从MSDN

用于构造字符串。插值字符串表达式看起来 比如包含表达式的模板字符串。一个插值 字符串表达式通过替换包含的字符串来创建字符串 表达式的ToString表示 结果。

ex :

 var name = "Sam";
 var msg = $"hello, {name}";

 Console.WriteLine(msg); // hello, Sam

可以在插值字符串中使用表达式

 var msg = $"hello, {name.ToLower()}";
 Console.WriteLine(msg); // hello, sam

它的好处是,您不需要像使用String.Format那样担心参数的顺序。

  var s = String.Format("{0},{1},{2}...{88}",p0,p1,..,p88);

现在如果你想移除一些参数你必须去更新所有的计数,现在不是这样了。

请注意,好的旧字符串。如果您想在格式中指定文化信息,格式仍然是相关的。

下面的示例强调了使用插值字符串相对于string.Format()在简洁和可读性方面的各种优点。它还显示{}内的代码像任何其他函数参数一样被求值,就像调用string.Format()一样。

using System;

public class Example
{
   public static void Main()
   {
      var name = "Horace";
      var age = 34;
      // replaces {name} with the value of name, "Horace"
      var s1 = $"He asked, \"Is your name {name}?\", but didn't wait for a reply.";
      Console.WriteLine(s1);

      // as age is an integer, we can use ":D3" to denote that
      // it should have leading zeroes and be 3 characters long
      // see https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-pad-a-number-with-leading-zeros
      //
      // (age == 1 ? "" : "s") uses the ternary operator to 
      // decide the value used in the placeholder, the same 
      // as if it had been placed as an argument of string.Format
      //
      // finally, it shows that you can actually have quoted strings within strings
      // e.g. $"outer { "inner" } string"
      var s2 = $"{name} is {age:D3} year{(age == 1 ? "" : "s")} old.";
      Console.WriteLine(s2); 
   }
}
// The example displays the following output:
//       He asked, "Is your name Horace?", but didn't wait for a reply.
//       Horace is 034 years old.