C# 中字串前的 @ 符號表示什麼?


它將字串標記為一個原文字串字面值

在 C# 中,使用特殊符號 @ 建立原文字串。@ 被稱為原文識別符號。如果一個字串包含了字首 @ 後面緊跟雙引號,那麼編譯器就會將該字串識別為原文字串,並對該字串進行編譯。@ 符號的主要優點是告訴字串建構函式忽略跳脫字元和換行符。

示例

 線上演示

using System;
using System.IO;
namespace DemoApplication{
   class Program{
      static void Main(string[] args){
         Console.WriteLine("test string
test string");          Console.WriteLine(@"test string
test string");          //Both the below statements are same.          string jsonString1 = File.ReadAllText(@"D:\Json.json");          string jsonString2 = File.ReadAllText("D:\Json.json");          Console.ReadLine();       }    } }

輸出

以上程式碼的輸出如下。

test string
test string
test string 
test string

更新日期: 2020 年 8 月 4 日

12,000+ 瀏覽

啟動你的職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.