Csharp/CSharp Tutorial/String/Verbatim literal strings

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Regular String Constants and Verbatim (@) String Constants

<source lang="csharp">using System; public class MainClass {

   public static void Main() {
       Console.WriteLine("\u0041BC");           // ABC 
       Console.WriteLine(@"\u0041BC");          // \u0041BC 
       Console.WriteLine("Say \"Hello\"!");     // Say "Hello"! 
       Console.WriteLine(@"Say ""Hello""!");    // Say "Hello"! 
       String s1 = @"Line 1 

and Line 2"; // Newline allowed only in verbatim string

       String s2 = "Line 1\nand Line 2";        // s1 and s2 are equal 
   }

}</source>

Verbatim literal strings: multiple line string

<source lang="csharp">using System;

class Example {

 public static void Main() {    
   Console.WriteLine(@"BBBBBB

AAAAA VVVVVVV ");

   Console.WriteLine(@"Here is some tabbed output: 

1 2 3 4 5 6 7 8 ");

 }    

}</source>

BBBBBB
AAAAA
VVVVVVV
Here is some tabbed output:
1       2       3       4
5       6       7       8

Verbatim literal strings: quote

<source lang="csharp">using System;

class Example {

 public static void Main() {    
   Console.WriteLine(@"Programmers say, ""I like C#."""); 
 }

}</source>

Programmers say, "I like C#."

Verbatim Strings

A verbatim string literal begins with an @, which is followed by a quoted string.

The contents of the quoted string are accepted without modification and can span two or more lines.


<source lang="csharp">using System; class MainClass {

   public static void Main()
   {
       string s = @"
       C: asdfasdf
       O: "Miss" &
       C: asdfasdf";
       
       Console.WriteLine(s);
   }

}</source>

C: asdfasdf
        O: "Miss" &
        C: asdfasdf