简体   繁体   English

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

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

I discovered that you can start your variable name with a '@' character in C#.我发现您可以在 C# 中以“@”字符开头变量名。 In my C# project I was using a web service (I added a web reference to my project) that was written in Java.在我的 C# 项目中,我使用了一个用 Java 编写的 Web 服务(我向我的项目添加了一个 Web 引用)。 One of the interface objects defined in the WSDL had a member variable with the name "params". WSDL 中定义的接口对象之一具有名为“params”的成员变量。 Obviously this is a reserved word in C# so you can't have a class with a member variable with the name "params".显然,这是 C# 中的保留字,因此您不能拥有具有名称为“params”的成员变量的类。 The proxy object that was generated contained a property that looked like this:生成的代理对象包含一个如下所示的属性:

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.我搜索了 VS 2008 c# 文档,但找不到任何相关信息。 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?那么变量/属性名称中“@”字符的确切含义或用法是什么?

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.带有@ 前缀的标识符称为逐字标识符。

It just lets you use a reserved word as a variable name.它只是让您使用保留字作为变量名。 Not recommended IMHO (except in cases like you have).不推荐恕我直言(除非像你这样的情况)。

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 (eg you can't use params but you can use @params instead, same with out/ref/any other keyword in the language specification).具体来说,它可以用于与保留关键字冲突的变量名(例如,您不能使用 params 但您可以使用 @params 代替,与语言规范中的 out/ref/任何其他关键字相同)。 Additionally it can be used for unescaped string literals;此外,它还可以用于未转义的字符串文字; this is particularly relevant with path constants, eg instead of path = "c:\\\\temp\\\\somefile.txt" you can write path = @"c:\\temp\\somefile.txt" .这与路径常量特别相关,例如,您可以编写path = @"c:\\temp\\somefile.txt"而不是path = "c:\\\\temp\\\\somefile.txt" path = @"c:\\temp\\somefile.txt" It's also really useful for regular expressions.它对于正则表达式也非常有用。

Unlike Perl's sigils, an @ prefix before a variable name in C# has no meaning.与 Perl 的 sigils 不同,C# 中变量名前的@前缀没有意义。 If x is a variable, @x is another name for the same variable.如果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;

The @ symbol allows you to use reserved keywords for variable name. @符号允许您使用保留关键字作为变量名。 like @int , @string , @double etc.@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";

It simply allows you to use reserved words as variable names.它只是允许您使用保留字作为变量名。 I wanted a var called event the other day.前几天我想要一个叫做event的 var。 I was going to go with _event instead, but my colleague reminded me that I could just call it @event instead.我本来打算用_event代替,但我的同事提醒我,我可以改为将其@event

Another use case are extension methods.另一个用例是扩展方法。 The first, special parameter can be distinguished to denote its real meaning with @this name.第一个特殊参数可以通过@this名称来区分以表示其真正含义。 An example:一个例子:

public static TValue GetValueOrDefault<TKey, TValue>(
    this IDictionary<TKey, TValue> @this,
    TKey key,
    TValue defaultValue)
    {
        if (!@this.ContainsKey(key))
        {
            return defaultValue;
        }

        return @this[key];
    }

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使用思想不是一种常见的做法

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字符@ 实际上不是标识符的一部分,因此标识符在其他语言中可能被视为普通标识符,没有前缀

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM