C# 中字符串前面的 @ 是什么意思?

csharpserver side programmingprogramming更新于 2025/10/2 12:22:17

它将字符串标记为逐字字符串字面值。

在 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

相关文章