1

I've declared a class like this:

public class Cell : IEquatable<Cell>
{
    public int id { get; }
    public SetOfCells group;
}

And each time I want to use group, Rider adds and @ before, like this:

foreach (Cell cell in c1.@group) {
    /* blabla */
}

Precision: c1.@group and c1.group both works. Why is that?

(if you can tell me the right words to google to find a valuable answer I'm interested, because I couldn't find one: "csharp property +"@" doesn't help...")

Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • I feel that is a weird behavior specific to Rider, because in Visual Studio, it doesn't detect that particular usage as the `group` keyword. It has to be used within a LINQ query for it to be detected as a keyword. – Timothy G. May 30 '21 at 15:09
  • @OlivierPons the `@` character used like this in C# is called 'verbatim identifier' or 'verbatim prefix': https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim – Arca Artem May 30 '21 at 15:20

1 Answers1

7

group is a keyword in C# (for the LINQ group clause), and @ is the "verbatim prefix" used by C# to use keywords as identifiers (e.g. int @int = 3;).

In this case, the @ is not necessary: group was added as a keyword in a later version of C#, so the C# compiler had to remain backwards-compatible with existing code and accept group in situations where other keywords such as int would not be allowed:

var int = 3;    // won't compile
var group = 3;  // compiles

group is a so-called "contextual keyword".

I cannot answer why Rider adds @ in front of group. I suspect that Rider tries to "play it safe" and adds a @ in front of every usage of a C# keyword as an identifier (as you have found out, it doesn't hurt).

A pragmatic solution would be to rename your member to Group (with a capitalized G).

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Probably an implementation decision in Rider favouring pragmatism over more accurate but also more costly implementation of various refactoring. Seems like a few people reported this in the past (5 years ago): https://youtrack.jetbrains.com/issue/RSRP-459185 – Arca Artem May 30 '21 at 15:27