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,寫着也麻煩


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM