C#中@字符d是個什么意思
更新時間:2023年05月04日 10:43:33 作者:你の貓
這篇文章主要介紹了C#中@字符d是個什么意思?具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
C#中@字符d是什么意思
C# string 字符串的前面可以加 @(稱作"逐字字符串")將轉(zhuǎn)義字符(\)當作普通字符對待,比如:
string str = @"C:\Windows";
等價于:
string str = "C:\\Windows";
@ 字符串中可以任意換行,換行符及縮進空格都計算在字符串長度之內(nèi)。
string str = @"<script type=""text/javascript""> ? ? <!-- ? ? --> </script>";
C#中@的3種作用
1.忽略轉(zhuǎn)義字符
例如:
string fileName = "D:\\文本文件\\text.txt";
使用@后
string fileName = @"D:\文本文件\text.txt";
2.讓字符串跨行
例如:
?? ?string strSQL = "SELECT * FROM HumanResources.Employee AS e" ? ?+ " INNER JOIN Person.Contact AS c" ? ?+ " ON e.ContactID = c.ContactID" ? ?+ " ORDER BY c.LastName";
使用@后
?? ?string strSQL = @"SELECT * FROM HumanResources.Employee AS e ?? ??? ??? ? ? ?INNER JOIN Person.Contact AS c ?? ??? ??? ? ? ?ON e.ContactID = c.ContactID ?? ??? ??? ? ? ?ORDER BY c.LastName";
3.在標識符中的用法
C#是不允許關鍵字作為標識符(類名、變量名、方法名、表空間名等)使用的,但如果加上@之后就可以了
例如:
?? ?public static void @static(int @int) ?{ ? ? ? ? ? ? if (@int > 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? System.Console.WriteLine("Positive Integer"); ? ? ? ? ? ? } ? ? ? ? ? ? else if (@int == 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? System.Console.WriteLine("Zero"); ? ? ? ? ? ? } ? ? ? ? ? ? else ? ? ? ? ? ? { ? ? ? ? ? ? ? ? System.Console.WriteLine("Negative Integer"); ? ? ? ? ? ? } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。