Scott Davies
Scott Davies

Reputation: 1379

Best practices for using @ in C#

While reading a book on C#, I have come across code that uses the @ to "overload" or use a C# keyword as an identifier. I am guessing this is not a good practice, as it leads to ambiguity. Am I correct in thinking this, or are there times where this should be used ?

Upvotes: 8

Views: 1052

Answers (8)

chikak
chikak

Reputation: 1732

You can safely avoid anything that creates confusion. :-)

The only place i use @ is with strings.

From MSDN: The advantage of @-quoting is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:

@"c:\Docs\Source\a.txt" // rather than "c:\\Docs\\Source\\a.txt"

Upvotes: 1

Martin Liversage
Martin Liversage

Reputation: 106826

As others have pointed out @-quoting can be very useful with strings. Here are some examples:

 // Not having to escape backslashes.
String a = @"C:\Windows\System32";

// Escaping quotes.
String b = @"The message is ""{0}"".";

// String with embedded newlines.
String c = @"An error occured.
Please try again later.
";

Upvotes: 4

Finglas
Finglas

Reputation: 15709

Never knew about this, but from what you say I'd avoid it.

The only time I use @ in C# is for pre-formatted strings.

@"String\no\need\to\escape\slashes"

Upvotes: 1

Gregory Higley
Gregory Higley

Reputation: 16558

I avoid it, except with extension methods, where I think it aids readability:

public static void Foo(this object @this)
{
     Console.WriteLine(@this.ToString());
}

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

I think it is a bad idea to have reserved keywords as variable names. IMHO it makes the code less readable. Although there are some cases where it could be useful. For example in an ASP.NET MVC View you could write:

<%= Html.TextBox("name", new { @class = "some_css_class" }) %>

Upvotes: 4

David
David

Reputation: 15360

I've used it in asp.net MVC Views where you are defining a css class using a HtmlHelper.

<%= Html.TextBox("Name", null, new { @class = "form-field" }) %>

Upvotes: 17

TheVillageIdiot
TheVillageIdiot

Reputation: 40497

I've only used and seen it used with the strings only, like:

string name=@"some funny name";

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062770

Indeed, it is almost always worth avoiding this. The main valid time it might be useful is when dealing with an external library (from another language) that has a class / etc that happens to be a C# keyword, but I'll admit I've never had this problem in reality. It helps that C# allows you to rename method argument names when overriding / implementing interfaces, so that avoids one common set of cases.

Another (arguably lazy) use is in code generation; if you always prefix fields / variables / etc with @theName, then you don't have to worry about special cases. Personally, I simply cross-check against a list of C# keywords / contextual keywords, and add something like underscore.

The @ symbol's other use (verbatim string literals) is @"much more useful".

Upvotes: 18

Related Questions