SIGN UP MEMBER LOGIN:    
ARTICLE

Verbatim strings in C# - Use of @ symbol in string literals

Posted by Hirendra Sisodiya Articles | Visual C# February 04, 2011
In this article we will understand use of the @ symbol in C# strings.
Reader Level:

A String can be created as a verbatim string. Verbatim strings start with @ symbol. The C# compiler understand this type of string as verbatim. Basically the @ symbol tells the string constructor to ignore escape characters and line breaks.

In this article we will understand use of the @ symbol in C# strings.

See here

String Str1 = "";

Str1 = "\\MyServer\TestFolder\NewFolder";

In statement above the compiler gives an error of 'Unrecognized escape sequence', but if we write the code like this:

str2 = @"\\MyServer\TestFolder\NewFolder";

the compiler does not generate an error.

Now we take an another example:

There are two strings, str1 and str2:

  String Str1 = "";

  String str2 = "";

  Str1 = "this is the \n test string";

 

Output of the str1 is :

 

 

this is the

 test string

 

  str2 = @"this is the \n test string";

output of the str2:

In the case of str2, the compiler will not process the \n when generating the output.

 

this is the \n test string

 

The outputs are different and we can easily understand the benefit of @ in strings in the C# language.

The main benefit of this symbol is that escape characters such as \n and \t will not be processed.

Note: MSDN says that a verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals.

Login to add your contents and source code to this article
share this article :
post comment
 

thank for share

Posted by ebook download Apr 07, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor