C# 关键字作为变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/860652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# keywords as a variable
提问by Icemanind
In VB.NET, you can surround a variable name with brackets and use keywords as variable names, like this:
在 VB.NET 中,您可以用括号将变量名括起来,并使用关键字作为变量名,如下所示:
Dim [goto] As String = ""
Is there a C# equivlent to doing this?
是否有 C# 相当于这样做?
采纳答案by Dour High Arch
string @string = "";
回答by Lasse V. Karlsen
Yes, prefix it with a @
是的,用@ 作为前缀
String @goto = "";
回答by andleer
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 but think the using an @
sign to name variables is ugly.
我不时使用后者,但认为使用@
符号来命名变量是丑陋的。