What does @ mean before a variable in C #?

firstly, this is not duplication. What does the @ symbol mean before the variable name in C #?

as group not a saved keyword.

I wrote the code and the editor suggested that I add @ before the group variable.

Any idea why?

 var group = GetDefaultGroup(ClientServiceCommon.Poco.Group); filteredPairs = @group.Pairs.ToList(); 
+7
source share
1 answer

As you correctly indicate, group not a reserved keyword (language developers try not to introduce new reserved keywords by adding new functions to the language so as not to break existing programs). This is, however, a contextual keyword: it becomes a keyword in LINQ query expressions. Resharper suggests that you rename the variable in question in order to avoid the ambiguities that might arise if this variable were used in query expressions.

From the specification:

7.16.1 Ambiguity of query expressions

Query expressions contain several “contextual keywords,” that is, identifiers that have special meaning in this context. In particular, it is, from, where, on, equals, to, let, orderby, ascending, descending, select, group and. To avoid ambiguities in query expressions caused by the mixed use of these identifiers as keywords or simple names, these identifiers are considered keywords when they occur anywhere within the query expression. For this purpose, a query expression is any expression starting with "from identifier", followed by any token other than ";", "=" or ",".

If you try the following, you will get a compiler error:

 var filteredParis = from pair in group.Pairs // group is now treated as a keyword select pair; 
+12
source

All Articles