C# 中变量名中@ 字符的用途/含义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/91817/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:33:48  来源:igfitidea点击:

What's the use/meaning of the @ character in variable names in C#?

提问by Remko Jansen

I discovered that you can start your variable name with a '@' character in C#. In my C# project I was using a web service (I added a web reference to my project) that was written in Java. One of the interface objects defined in the WSDL had a member variable with the name "params". Obviously this is a reserved word in C# so you can't have a class with a member variable with the name "params". The proxy object that was generated contained a property that looked like this:

我发现您可以在 C# 中以“@”字符开头变量名。在我的 C# 项目中,我使用了一个用 Java 编写的 Web 服务(我向我的项目添加了一个 Web 引用)。WSDL 中定义的接口对象之一具有名为“params”的成员变量。显然,这是 C# 中的保留字,因此您不能拥有具有名称为“params”的成员变量的类。生成的代理对象包含一个如下所示的属性:

public ArrayList @params {
    get { return this.paramsField; }
    set { this.paramsField = value; }
}

I searched through the VS 2008 c# documentation but couldn't find anything about it. Also searching Google didn't give me any useful answers. So what is the exact meaning or use of the '@' character in a variable/property name?

我搜索了 VS 2008 c# 文档,但找不到任何相关信息。搜索谷歌也没有给我任何有用的答案。那么变量/属性名称中“@”字符的确切含义或用法是什么?

采纳答案by Atif Aziz

Straight from the C# Language Specification, Identifiers (C#):

直接来自C# Language Specification, Identifiers (C#)

The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier.

前缀“@”允许使用关键字作为标识符,这在与其他编程语言交互时非常有用。字符@ 实际上不是标识符的一部分,因此标识符在其他语言中可能被视为普通标识符,没有前缀。带有@ 前缀的标识符称为逐字标识符。

回答by rslite

It just lets you use a reserved word as a variable name. Not recommended IMHO (except in cases like you have).

它只是让您使用保留字作为变量名。不推荐恕我直言(除非像你这样的情况)。

回答by Mark Embling

It simply allows you to use reserved words as variable names. I wanted a var called eventthe other day. I was going to go with _eventinstead, but my colleague reminded me that I could just call it @eventinstead.

它只是允许您使用保留字作为变量名。前event几天我想要一个 var 。我本来打算去的_event,但我的同事提醒我,我可以直接打电话给它@event

回答by Tomer Gabel

In C# the at (@) character is used to denote literals that explicitly do not adhere to the relevant rules in the language spec.

在 C# 中,at (@) 字符用于表示明确不遵守语言规范中相关规则的文字。

Specifically, it can be used for variable names that clash with reserved keywords (e.g. you can't use params but you can use @params instead, same with out/ref/any other keyword in the language specification). Additionally it can be used for unescaped string literals; this is particularly relevant with path constants, e.g. instead of path = "c:\\temp\\somefile.txt"you can write path = @"c:\temp\somefile.txt". It's also really useful for regular expressions.

具体来说,它可以用于与保留关键字冲突的变量名(例如,您不能使用 params 但您可以使用 @params 代替,与语言规范中的 out/ref/任何其他关键字相同)。此外,它还可以用于未转义的字符串文字;这与路径常量特别相关,例如,path = "c:\\temp\\somefile.txt"您可以编写path = @"c:\temp\somefile.txt". 它对于正则表达式也非常有用。

回答by Atif Aziz

If we use a keyword as the name for an identifier, we get a compiler error “identifier expected, ‘Identifier Name' is a keyword” To overcome this error, prefix the identifier with “@”. Such identifiers are verbatim identifiers. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix

如果我们使用关键字作为标识符的名称,则会出现编译器错误“预期标识符,'标识符名称'是关键字”要克服此错误,请在标识符前加上“@”。这样的标识符是逐字标识符。字符@ 实际上不是标识符的一部分,因此标识符在其他语言中可能被视为普通标识符,没有前缀

回答by Colonel Panic

Unlike Perl's sigils, an @prefix before a variable name in C# has no meaning. If xis a variable, @xis another name for the same variable.

与 Perl 的 sigils 不同@,C# 中变量名前的前缀没有意义。如果x是变量,@x则是同一变量的另一个名称。

> string x = "abc";
> Object.ReferenceEquals(x, @x).Dump();
True

But the @prefix does have a use, as you've discovered - you can use it to clarify variables names that C# would otherwise reject as illegal.

但是@前缀确实有用途,正如您所发现的 - 您可以使用它来阐明 C# 否则会拒绝为非法的变量名称。

> string string;
Identifier expected; 'string' is a keyword

> string @string;

回答by BartoszKP

Another use case are extension methods. The first, special parameter can be distinguished to denote its real meaning with @thisname. An example:

另一个用例是扩展方法。第一个,特殊参数可以通过@this名称来区分以表示其真正含义。一个例子:

public static TValue GetValueOrDefault<TKey, TValue>(
    this IDictionary<TKey, TValue> @this,
    TKey key,
    TValue defaultValue)
    {
        if ([email protected](key))
        {
            return defaultValue;
        }

        return @this[key];
    }

回答by Mina Gabriel

You can use it to use the reserved keywords as variable name like

您可以使用它来使用保留关键字作为变量名,如

 int @int = 3; 

the compiler will ignores the @and compile the variable as int

编译器将忽略@变量并将其编译为int

it is not a common practice to use thought

使用思想不是一种常见的做法

回答by Umar Abbas

The @symbol allows you to use reserved keywords for variable name. like @int, @string, @doubleetc.

@符号允许您使用保留关键字作为变量名。像@int@string@double等等。

For example:

例如:

string @public = "Reserved Keyword used for me and its fine";

The above code works fine, but below will not work:

上面的代码工作正常,但下面的代码不起作用:

string public = "This will not compile";