11,578 questions
There's no benefit other than descriptiveness, the '@' syntax is just an escape hatch in case you really need to.
One example of when you might want to do this might be for properties on a class that you're deserialising some JSON into:
using System.Text.Json;
using System.Text.Json.Serialization;
Player player = JsonSerializer.Deserialize<Player>(@"{
""class"": ""Warrior""
}");
class Player {
public string @class { get; set; }
}
You may not be able to control the format of the incoming JSON. Although even in this scenario you could get around it by using [JsonPropertyName]
:
class Player {
[JsonPropertyName("class")]
public string ClassName { get; set; }
}
It's more of a preference, but I tend to try and avoid keyword collisions and just rename my identifiers.