What's the use/meaning of the @ character in v

2018-12-31 04:23发布

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?

9条回答
浅入江南
2楼-- · 2018-12-31 05:06

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";
查看更多
余生请多指教
3楼-- · 2018-12-31 05:06

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

查看更多
明月照影归
4楼-- · 2018-12-31 05:15

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

查看更多
梦该遗忘
5楼-- · 2018-12-31 05:17

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.

查看更多
看淡一切
6楼-- · 2018-12-31 05:20

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

查看更多
听够珍惜
7楼-- · 2018-12-31 05:23

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.

查看更多
登录 后发表回答