Coder Perfect

In C#, what does the @ in front of a string mean?


This is a C# (or possibly question, but I’m attempting to figure out what the difference is between these declarations:

string hello = "hello";


string hello_alias = @"hello";

The length attributes are the same whether you print to the console or not.

Asked by Klaw

Solution #1

It indicates that the string is a verbatim string literal, meaning that anything in it that would typically be treated as an escape sequence is ignored.

As a result, “C:UsersRich” and “@”C:UsersRich” are interchangeable.

There is one exception: the double quote requires an escape sequence. You must use two double quotes in a row to escape a double quotation. @”””” for example, evaluates to “.

Answered by Richard Ev

Solution #2

It’s a literal string that’s been copied word for word. It indicates that fleeing isn’t used. Consider the following example:

string verbatim = @"foo\bar";
string regular = "foo\\bar";

The contents of both verbatim and regular are the same in this case.

It also supports multi-line content, which is useful for SQL:

string select = @"
WHERE Name='Baz'";

The only escape required for verbatim string literals is to obtain a double quotation (“), which is obtained by doubling it:

string verbatim = @"He said, ""Would you like some coffee?"" and left.";
string regular = "He said, \"Would you like some coffee?\" and left.";

Answered by Jon Skeet

Solution #3

Another meaning of a ‘@’ is that it lets you to use reserved keywords as variable names when used in front of a variable declaration.

For example:

string @class = "something";
int @object = 1;

I’ve only come across a couple of viable applications for this. When you wish to perform something like this, you should use ASP.NET MVC.

<%= Html.ActionLink("Text", "Action", "Controller", null, new { @class = "some_css_class" })%>

This would result in an HTML link like this:

<a href="/Controller/Action" class="some_css_class">Text</a>

Otherwise, you’d have to use ‘Class,’ which isn’t a reserved keyword, but the uppercase ‘C’ isn’t compliant with HTML standards and simply doesn’t look right.

Answered by JulianR

Solution #4

Since you specifically requested VB, I’ll merely point out that this verbatim string syntax is only available in C#. Rather, in VB, all strings are verbatim (save for the fact that, unlike C# verbatim strings, they cannot contain line breaks):

Dim path = "C:\My\Path"
Dim message = "She said, ""Hello, beautiful world."""

There are no escape sequences in VB (save for the doubling of the quote character, like in C# verbatim strings), which complicates a few things. To write the following code in VB, for example, you’ll need to utilize concatenation (or any of the other ways to construct a string)

string x = "Foo\nbar";

This would be written in VB as follows:

Dim x = "Foo" & Environment.NewLine & "bar"

(The VB string concatenation operator is &, but + can also be used.)

Answered by Konrad Rudolph

Solution #5

Regular string literals and verbatim string literals are two types of string literals supported by C#.

A standard string literal, like “hello,” consists of zero or more letters contained in double quotes and may contain both simple escape sequences (like t for the tab character) and hexadecimal and Unicode escape sequences.

An @ character precedes a double-quote character, zero or more characters, and a closing double-quote character in a verbatim string literal. @”hello” is a simple example. The characters between the delimiters are interpreted verbatim in a verbatim string literal, with the exception of a quote-escape-sequence. In verbatim string literals, basic escape sequences, as well as hexadecimal and Unicode escape sequences, are not handled. A string literal that is verbatim can span many lines.

Answered by Ed Guiness

Post is based on