舉個例子,依然拿我之前那篇用過的學生(簡化)
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;
結果: