Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does @ before variabe means in c#?

Tags:

c#

resharper

first, it's not a duplication of What does the @ symbol before a variable name mean in C#?

as group is not a preserved keyword.

I wrote some code and the resharper suggested me to add @ before the variable group.

Any idea why?

var group = GetDefaultGroup(ClientServiceCommon.Poco.Group);

filteredPairs = @group.Pairs.ToList();
like image 901
Elad Benda Avatar asked Feb 12 '13 14:02

Elad Benda


Video Answer


1 Answers

As you correctly point out, group is not a reserved keyword (the language designers try hard not to introduce new reserved keywords when they add new features to the language in order to not break existing programs). It is, however, a contextual keyword: it becomes a keyword within LINQ query expressions. Resharper is suggesting you rename the variable in question to avoid ambiguities that would result if this variable were used within query expressions.

From the language specification:

7.16.1 Ambiguities in query expressions

Query expressions contain a number of “contextual keywords”, i.e., identifiers that have special meaning in a given context. Specifically these are from, where, join, on, equals, into, let, orderby, ascending, descending, select, group and by. In order to avoid ambiguities in query expressions caused by mixed use of these identifiers as keywords or simple names, these identifiers are considered keywords when occurring anywhere within a query expression. For this purpose, a query expression is any expression that starts with “from identifier” followed by any token except “;”, “=” or “,”.

If you tried to do the following, you would get a compiler error:

var filteredParis = from pair in group.Pairs // group is now treated as a keyword
                    select pair;
like image 146
Ani Avatar answered Sep 24 '22 02:09

Ani