tags:

views:

359

answers:

8

Hi,

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 ?

+17  A: 

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".

Marc Gravell
Thanks Marc! This is really helpful.
Scott Davies
"\\Is\\VERY\\useful" @"don't\you\think?"
Vinko Vrsalovic
One use case for @ I've encountered is in ASP.NET MVC when you need to pass an anonymous type with a property with name of a C# keyword like `Html.Action(... , new {..., @class = "Test" });`
Mehrdad Afshari
@Mehrdad: check David Liddle's answer.
Fredrik Mörk
@Fredrick: Wow! I didn't see that. How similar!
Mehrdad Afshari
+1  A: 

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

string name=@"some funny name";
TheVillageIdiot
I think it's more usable when the string contains \ so that you don't need to escape it.
Jean Azzopardi
+1  A: 

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"
Finglas
I think you meant backslashes
Josef
Indeed I did ;)
Finglas
+15  A: 

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" }) %>
David Liddle
Nice example...
Marc Gravell
+3  A: 

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" }) %>
Darin Dimitrov
A: 

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

public static void Foo(this object @this)
{
     Console.WriteLine(@this.ToString());
}
Gregory Higley
would you not think that code actually causes more confusion given that you are already defining 'this'? i would prefer to just use a naming convention like 'this object o', like people define 'i' as the variable name for ints. for (int i = 0; i >...
David Liddle
I don't think it causes any confusion. Remember, when you're defining an extension method, one of the parameters serves as the equivalent of "this" in an ordinary method. I think calling it @this emphasizes that fact, particularly when glancing at the code.
Gregory Higley
Even better would be to come up with a parameter name that actually means something. :)
Guffa
Given that this is a rather obscure feature of C#, I think it will create confusion. Conveying that the parameter is equivalent to "this" in a normal method could better be served by writing "_this", which is universally understood to be a plain name, whereas "@" is a metacharacter and will lead to head-scratching, googling and wasted time.
harms
+1  A: 

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"

chikak
+4  A: 

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.
";
Martin Liversage
I tend to use this all the time. Much easier then escaping the characters.
corymathews