Csharp/CSharp Tutorial/String/Verbatim literal strings

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

Regular String Constants and Verbatim (@) String Constants

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 
    }
}

Verbatim literal strings: multiple line string

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 
"); 
    
  }    
}
BBBBBB
AAAAA
VVVVVVV
Here is some tabbed output:
1       2       3       4
5       6       7       8

Verbatim literal strings: quote

using System; 
 
class Example {    
  public static void Main() {    
    Console.WriteLine(@"Programmers say, ""I like C#."""); 
  }
}
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.


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