52 lines
1.8 KiB
Plaintext
52 lines
1.8 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Since C# 5.0, `async` and `await` are https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/#contextual-keywords[contextual keywords]. Contextual keywords do have a particular meaning in some contexts, but are not reserved and therefore can be used as variable names.
|
|
|
|
[source,csharp,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
int await = 42; // Noncompliant, but compiles
|
|
int async = 42; // Noncompliant, but compiles
|
|
----
|
|
|
|
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords[Keywords], on the other hand, are always reserved and therefore are not valid variable names.
|
|
|
|
[source,csharp]
|
|
----
|
|
int abstract = 42; // Error CS1585: Member modifier 'abstract' must precede the member type and name
|
|
int foreach = 42; // Error CS1519: Invalid token 'foreach' in class, struct, or interface member declaration
|
|
----
|
|
|
|
To avoid any confusion, it is best to not use `async` and `await` as identifiers.
|
|
|
|
[source,csharp,diff-id=1,diff-type=compliant]
|
|
----
|
|
int someVariableName = 42;
|
|
int someOtherVariableName = 42;
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/#contextual-keywords[Contextual Keywords - MSDN]
|
|
* https://learn.microsoft.com/en-us/dotnet/csharp/asynchronous-programming/[Asynchronous programming - MSDN]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Rename "xxx" to not use a contextual keyword as an identifier.
|
|
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== is related to: S1190
|
|
|
|
=== on 12 Jan 2015, 15:02:43 Freddy Mallet wrote:
|
|
I would tend to associate this coding rule to the characteristic "Portability -> Language related Portability" and also to the tag "obsolete"
|
|
|
|
endif::env-github,rspecator-view[]
|