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

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

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

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

string str = $"text\";

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

string str = $"text";

我使用Visual studio 2015 CTP。


当前回答

$语法很好,但有一个缺点。

如果你需要一个字符串模板,在类级别上声明为field…它本该在一个地方。

然后你必须在同一层次上声明变量…这一点都不酷。

使用字符串要好得多。这类事情的格式语法

class Example1_StringFormat {
 string template = $"{0} - {1}";

 public string FormatExample1() {
   string some1 = "someone";
   return string.Format(template, some1, "inplacesomethingelse");
 }

 public string FormatExample2() {
   string some2 = "someoneelse";
   string thing2 = "somethingelse";
   return string.Format(template, some2, thing2);
 }
}

全局变量的使用并不是很好,除此之外,它也不适用于全局变量

 static class Example2_Format {
 //must have declaration in same scope
 static string some = "";
 static string thing = "";
 static string template = $"{some} - {thing}";

//This returns " - " and not "someone - something" as you would maybe 
//expect
 public static string FormatExample1() {
   some = "someone";
   thing = "something";
   return template;
 }

//This returns " - " and not "someoneelse- somethingelse" as you would 
//maybe expect
 public static string FormatExample2() {
   some = "someoneelse";
   thing = "somethingelse";
   return template;
 }
}

其他回答

它表示字符串插值。

它会保护你,因为它在字符串求值上增加了编译时间保护。

你将不再得到string.Format("{0}{1}",secondParamIsMissing)异常

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

例子:

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

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

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

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

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

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

它比绳子更方便。格式,你也可以在这里使用智能感知。

下面是我的测试方法:

[TestMethod]
public void StringMethodsTest_DollarSign()
{
    string name = "Forrest";
    string surname = "Gump";
    int year = 3; 
    string sDollarSign = $"My name is {name} {surname} and once I run more than {year} years."; 
    string expectedResult = "My name is Forrest Gump and once I run more than 3 years."; 
    Assert.AreEqual(expectedResult, sDollarSign);
}

下面的示例强调了使用插值字符串相对于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.