使用者工具

網站工具


visual_c_sharp

這是本文件的舊版!


Visual C# 研究

string

[code] string a = “hello, world”; hello, world string b = @“hello, world”; hello, world string c = “hello \t world”; hello world string d = @“hello \t world”; hello \t world string e = “Joe said \”Hello\“ to me”; Joe said “Hello” to me string f = @“Joe said ”“Hello”“ to me”; Joe said “Hello” to me string g = “\\\\server\\share\\file.txt”; \\server\share\file.txt string h = @“\\server\share\file.txt”; \\server\share\file.txt string i = “one\r\ntwo\r\nthree”; string j = @“one two three”; [/code]

上面是msdn給的例子,說明了加不加@的差別, 基本上加上@就是告訴編譯器,接下來的這個字串我會全權負責,請編譯器不要自做聰明,只要如實照做就好了 如果你不加@,只要有反斜線,編譯器會以跳脫字元去解讀,比如像\t,就是按一次tab鍵的意思. 而使用@要注意的是上例的string f,我如果要顯示“時,要用”“來取代.

C# @符號(小老鼠)的用處

1.限定跳脫字串 用 @ 符號加在字串前面表示其中的轉義字元“不”被處理。 例如”D:\TEMP“路徑下的text.txt檔,不加@符號的話寫法如下: string fileName= “D:\\TEMP\\text.txt”; 如果使用@符號就會比較簡單: string fileName= @“D:\TEMP\text.txt”;   2.讓字串連接跨行 不使用@符號,一種寫法是這樣的: [code] string strSQL= “SELECT * FROM HumanResources.Employee AS e” +            ” INNER JOIN Person.Contact AS c“ + ” ON e.ContactID = c.ContactID“ +            ” ORDER BY c.LastName“; [/code] 加上@符號後就可以直接換行了: [code] string strSQL= @“SELECT * FROM HumanResources.Employee AS e         INNER JOIN Person.Contact AS c         ON e.ContactID = c.ContactID         ORDER BY c.LastName”;  [/code] 3.在識別字中的用法 C#是不允許關鍵字作為識別字(類名、變數名、方法名、表空間名等)使用的,但如果加上@之後就可以了。 [code] 例如 string @string=“abcdef”;      int @int = 123456789; [/code] 來自 <https://blog.xuite.net/david670919/twblog/499762471>

visual_c_sharp.1719298848.txt.gz · 上一次變更: 2024/06/25 15:00 由 don