天天看點

C#:$符号和@符号的用法介紹

c#中@的三種用法:

1.忽略轉義字元 \

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

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

2.字元串跨行

string str = "line one"

  + "line two"

  + "line three"

  + "line fore";

string str = @"line one

  line two

  line three

  line fore";      

3.将關鍵字作為辨別符使用

C#是不允許關鍵字作為辨別符(類名、變量名、方法名、表空間名等)使用的,但如果加上@之後就可以了

int @int = 1;      

$的作用

簡化string.Format()寫法,行駛Format的作用

string name = "KaiSarH";

int age = 0;

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

可簡寫為

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

Else

如果在字元串中想要輸出引号,不管是"還是’,都需要在其前面加一個轉義字元\