string 字符串前加“$”“@”“$@”分别的作用

举个例子,依然拿我之前那篇用过的学生(简化)

public class Student
{
    private int id;

    public int ID
    {
        get { return id; }
        set { id = value; }
    }
}

其他前提

Student student = new Student() { ID = 1 };

List<string> arr = new List<string>();

分别回车看效果

string a = @"12345
6789";
arr.Add(a);
string b = $"12345" +
                $"6789";
arr.Add(b);
string c = $@"12345
6789";
arr.Add(c);

结果:

所以@和$@都可以写多行连续的字符串,一般写很长的连表查询sql语句的时候用的多
a = @"{nameof(Student.ID)} = {student.ID}";
arr[0] = a;
b = $"{nameof(Student.ID)} = {student.ID}";
arr[1] = b;
c = $@"{nameof(Student.ID)} = {student.ID}";
arr[2] = c;

结果:

所以$和$@都可以将值直接放在字符串内,就不用每次写成这样nameof(Student.ID) + " = " + student.ID,写着也麻烦
原文地址:https://www.cnblogs.com/AlinaL/p/13322492.html