C#中的@和$ 占位符


c#中@的三種用法:

1.忽略轉移字符

string str = "C:\\windows\\system32";

string str = @"C:\windows\system32";

2.字符串跨行

string str = "SELECT * FROM Employee AS e"

  + "INNER JOIN Contact AS c"

  + "ON e.ContactID = c.ContactID"

  + "ORDER BY c.LastName";

string str = @"SELECT * FROM Employee AS e

  INNER JOIN Contact AS c"

  ON e.ContactID = c.ContactID"

  ORDER BY c.LastName";

3.將關鍵字作為標識符使用

int @int = 1;

 

c#中$的使用

簡化string.Format()寫法

string name = "上帝";

int age = 0;

string str = string.Format("my name is {0},I`m {1} years old",name,age);

可簡寫為

string str = $"my name is {name},I`m {age} years old";

 

需要輸出"時使用\"

需要輸出{}時使用{{ }}

string str = $"my \"name\" is {{{name}}},I`m {age} years old";


免責聲明!

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



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