Verbatim Strings
In the first part of this article, I mentioned that you could use the @ symbol to use a keyword as
an identifier—if you were so inclined, you could create variables named, for example, @if, @string,
and @true. (To repeat what I said back in Part I, just
because one can do something, doesn’t mean it is a good idea.)
In the context of strings, the @ symbol is used to create verbatim string literals. The @ symbol
tells the string constructor to use the string literal that follows it “literally”—even if it includes
escape characters or spans multiple lines.
This comes in handy when working with directory paths (without the @, you would have to double each
backslash). For example, the following two strings are equivalent:
string noat = "\\\\BIGSERVER\\C";
string withat = @"\\BIGSERVER\C";
Here’s how you could use the @ symbol to take the place of the \r\n escape sequence of control
characters, which means “carriage return, new line”:
private void btnVer_Click(object sender, System.EventArgs e) {
string str =
  @"It's fun
 to be
   split into a number
    of lines!";
txtDoIt.Text = str;
}
If you assign the verbatim string to a TextBox that has its Multiline property set to True, you’ll see that
the line breaks (and white spaces) are preserved exactly as entered in the verbatim string.
If you are curious, here’s how you’d create the same string as a non-verbatim string, using the control
characters and string concatenation:
string str = "It's fun\r\n  to be\r\n"+
"      split into a number\r\n" +
"        of lines!";
txtDoIt.Text = str;
|
|
Search Engine Optimization
 
Syndication Viewer
Our Web host:
IX WebHosting
|