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

发布于 2024-07-05 17:37:07 字数 412 浏览 15 评论 0原文

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

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

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

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

你爱我像她 2024-07-12 17:37:08

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

 int @int = 3; 

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

这不是使用思想的常见做法

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

it is not a common practice to use thought

爱情眠于流年 2024-07-12 17:37:08

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

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

稚然 2024-07-12 17:37:08

另一个用例是扩展方法。 第一个特殊参数可以通过@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];
    }

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

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

        return @this[key];
    }
笔落惊风雨 2024-07-12 17:37:08

它只是允许您使用保留字作为变量名。 前几天我想要一个名为 event 的 var。 我原本打算使用 _event 来代替,但我的同事提醒我,我可以将其命名为 @event

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

七颜 2024-07-12 17:37:07

@ 符号允许您使用变量名称的保留关键字。 如 @int@string@double 等。

例如:

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

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

string public = "This will not compile";

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

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";
江城子 2024-07-12 17:37:07

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

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

但正如您所发现的,@ 前缀确实有一个use - 您可以使用它来澄清 C# 会拒绝的变量名称,否则这些变量名称是非法的。

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

> string @string;

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

> 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.

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

> string @string;
寄离 2024-07-12 17:37:07

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

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

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

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.

少女的英雄梦 2024-07-12 17:37:07

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

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

゛清羽墨安 2024-07-12 17:37:07

直接来自 C# 语言规范,< a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specation/lexical-struct#identifiers" rel="noreferrer">标识符 (C#)
:

前缀“@”允许使用
关键字作为标识符,即
与其他人交互时很有用
编程语言。 人物 @
实际上并不属于
标识符,因此标​​识符可能是
在其他语言中被视为正常
标识符,不带前缀。 一个
带有@前缀的标识符被称为
逐字标识符。

Straight from the 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文