C#逐字字符串字面量– @用法介绍

2021年3月13日16:39:16 发表评论 1,163 次浏览

在C#中, 使用特殊符号创建逐字字符串@。 @被称为逐字标识符。如果一个字符串包含@作为前缀, 后跟双引号, 则编译器将该字符串标识为逐字字符串, 然后编译该字符串。 @符号的主要优点是告诉字符串构造函数忽略转义符和换行符。 @符号主要有以下三种用法:

使用1:

关键字作为标识符

该符号允许使用

关键字作为标识符

。 @符号是关键字的前缀, 因此编译器将关键字作为标识符, 没有任何错误, 如下例所示:

例子:

// C# program to illustrate
// the use of @ by using keyword
// as an identifier
using System;
  
public class GFG {
  
     // Main method
     static public void Main()
     {
  
         // Creating and initializing the array
         // here 'for' keyword is used as 
         // an identifier by using @ symbol
         string [] @ for = { "C#" , "PHP" , "Java" , "Python" };
  
                 // as and for keywords is 
                 // as an identifier
                 // using @ symbol
                 foreach ( string @ as in @ for )
                 {
                     Console.WriteLine( "Element of Array: {0}" , @ as );
                 }
     }
}

输出如下:

Element of Array: C#
Element of Array: PHP
Element of Array: Java
Element of Array: Python

使用2: 用于在字符串文字中打印转义序列, 以及在没有任何转义序列的字符串文字中使用换行符等。

如果有人将转义序列" \\"(用于反斜杠), " \ u"(Unicode转义序列), "\X"(十六进制转义序列)等, 而不使用@符号, 则这些字符串将由编译器自动解释。但是, ""(双引号)不是按字面意义解释的。它就像一个字符串插值。让我们看看带有@符号和不带有@符号的不同情况。

情况1:

// taking a string literal and 
// try to print double quotes
string str1 = """";

// printing output
// this will give compile
// time error as Unexpected 
// symbol `' 
Console.WriteLine(str1);

在上面的程序中, 双引号内的双引号作为字符串文字被解释为单引号。

情况2:

// taking a string literal prefixes
// with @ and try to print double quotes
string str1 = @"""";

// printing output
// this will output as "
Console.WriteLine(str1);

在上面的程序中, 输出是双引号(")不""

情况3:

// taking a string in which we are storing 
// some location of file but \Testing will 
// interpreted as eascape sequence \T 
// similarly \N
string str1 = "\\C:\Testing\New\Target";

// printing str1
// this will give compile time error as
// Unrecognized escape sequence `\T'
// Unrecognized escape sequence `\N'
// Unrecognized escape sequence `\T'
Console.WriteLine(str1);

情况4:

// taking a string and prefix literal with @ symbol. 
// Storing some location of file 
string str1 = @"\\C:\Testing\New\Target";

// printing str1 will give output as 
// \\C:\Testing\New\Target
Console.WriteLine(str1);

程序:

// C# program to illustrate
// the use of @ in terms of 
// escape sequences and new 
// line and tab
using System;
  
public class GFG {
  
     // Main method
     static public void Main()
     {
  
         // If you use the below commented
         // the part then this will give
         // Unrecognized escape sequence error
         // string S1 = "\\welcome \to lsbin \ portal \";
         // Console.WriteLine("String 1 is :{0}", S1);
  
         // By using @ in the given string 
         // it runs smoothly because
         // @ symbol tells the compiler to
         // ignore all escape sequences
         string S2 = @"\\welcome \to lsbin \ portal \" ;
         Console.WriteLine( "String 2 is: {0}" , S2);
  
         // printing new line character in string literal
         // but it will make the string to break  
         // into a new line, see output
         string S3 = "This is \n C# non verbatim string" ;
         Console.WriteLine( "String 3 is :{0}" , S3);
  
         // By using @ symbol /n does not processed
         string S4 = @"This is \n C# verbatim string" ;
         Console.WriteLine( "String 4 is :{0}" , S4);
  
         // printing a string literal contains 
         // tabs and new line without using 
         // any escape sequence
         Console.WriteLine( @"Without Tab Sequence and New Line Character
                                C          C++      Java       Python" );
     }
}

输出如下:

String 2 is: \\welcome \to lsbin \ portal \
String 3 is :This is 
 C# non verbatim string
String 4 is :This is \n C# verbatim string
Without Tab Sequence and New Line Character
                               C          C++      Java       Python

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: