Friday, January 11, 2013

Using @-prefix in C#.NET


C# introduces the @-prefixed string literal notation termed a verbatim string. Using verbatim strings, you disable the processing of a literal’s escape characters. This can be most useful when working
with strings representing directory and network paths. Therefore, rather than making use of \\ escape
characters, you can simply write the following:

// The following string is printed verbatim
// thus, all escape characters are displayed.

Console.WriteLine(@"C:\MyApp\bin\debug");

Also note that verbatim strings can be used to preserve white space for strings that flow over multiple lines:

// White space is preserved with verbatim strings.

string myLongString = @"This is a very
very
    very
        long string";
Console.WriteLine(myLongString);

You can also insert a double quote into a literal string by doubling the " token, for example:
Console.WriteLine(@"Cerebus said ""Darrr! Pret-ty sun-sets""");

No comments:
Write comments
Recommended Posts × +