C# 用@ 前缀参数名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1038674/
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# prefixing parameter names with @
提问by Frederik Gheysels
Possible Duplicate:
What does the @ symbol before a variable name mean in C#?Duplicate:
可能的重复:
C# 中变量名前的@ 符号是什么意思?复制:
Sometimes I see some C# code where a method-parameter is prefixed with an @, like this:
有时我会看到一些 C# 代码,其中方法参数以 @ 为前缀,如下所示:
public static void SomeStaticMethod( SomeType @parameterName ) { }
What is the meaning of this ? Does it has some significant special meaning ?
这是什么意思 ?它有什么重要的特殊意义吗?
I am creating an EventListener in NHibernate, and when I let VS.NET generate the interface methods, it generates the OnPostLoad method like this:
我在 NHibernate 中创建了一个 EventListener,当我让 VS.NET 生成接口方法时,它会生成这样的 OnPostLoad 方法:
public class PostLoadEventListener : IPostLoadEventListener
{
public void OnPostLoad( PostLoadEvent @event )
{
}
}
Why is this ?
为什么是这样 ?
采纳答案by Hugoware
Try and make a variable named classand see what happens -- You'll notice you get an error.
尝试创建一个名为class的变量,看看会发生什么——您会注意到出现错误。
This lets you used reserved words as variable names.
这让您可以使用保留字作为变量名。
Unrelated, you'll also notice strings prefixed with @ as well -- This isn't the same thing...
不相关,您还会注意到以 @ 为前缀的字符串——这不是一回事......
string says = @"He said ""This literal string lets me use \ normally
and even line breaks"".";
This allows you to use 'literal' value of the string, meaning you can have new lines or characters without escapes, etc...
这允许您使用字符串的“文字”值,这意味着您可以使用没有转义的新行或字符等...
回答by Timothy Carter
event is a C# keyword, the @ is an escape character that allows you to use a keyword as a variable name.
event 是一个 C# 关键字,@ 是一个转义字符,它允许您将关键字用作变量名。
回答by Rad
The @ prefix allows you to use reserved words like class, interface, events, etc as variable names in C#. So you can do
@ 前缀允许您使用保留字,如类、接口、事件等作为 C# 中的变量名。所以你可以做
int @int = 1