tags:

views:

343

answers:

3

In VB.NET, you can surround a variable name with brackets and use keywords as variable names, like this:

Dim [goto] As String = ""

Is there a C# equivlent to doing this?

+24  A: 
string @string = "";
Dour High Arch
This is baaad practice IMHO. Variable names should be descriptive. @string is not descriptive.
Daniel Schaffer
@Daneil: it might be bad practice, but it was a very good answer.
Fredrik Mörk
I wasn't debating that, I still upvoted... perhaps it was a better comment for the question.
Daniel Schaffer
The purpose of the @ prefix is to allow for interoperability with other .NET languages, see http://stackoverflow.com/questions/724912/does-the-prefix-for-delegates-have-any-special-meaning/724951#724951
0xA3
Strange I never realized that string is a keyword... I always thought that it is an identifier alias, but it is apparently a full-rights keyword.
DrJokepu
All the keywords that are aliases for the system types (int, uint, etc.) are normal C# keywords so they all need to be prefixed.
Lasse V. Karlsen
This is bad practice, but a good answer.
Nate Bross
divo: also, compatability with assemblies compiled against previous versions of C# since new keywords are introduced all the time.
DrJokepu
@DrJokepu: Good point, didn't think about that.
0xA3
@DrJokepu actually no new reserved keywords have been added, only contextual keywords. So a declaration like "var var = 15;" is valid without the @. It seems this has been intentionally done to avoid backwards compatibility issues. http://blogs.msdn.com/ericlippert/archive/2009/05/11/reserved-and-contextual-keywords.aspx
Timothy Carter
+9  A: 

Yes, prefix it with a @

String @goto = "";
Lasse V. Karlsen
+2  A: 

Prefix your variable with the @ sign

string @class = "fred";

The @ sign can also be used to prefix a non-escaped string literal:

string a = "fred\"; \\ invalid
string b = @"fred\"; \\ valid. the backslash is part of the literal 'fred\'

I use the latter from time to time butthink the using an @ sign to name variables is ugly.

Andrew Robinson