2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-01 13:18:49 +02:00
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.
2021-04-28 16:49:39 +02:00
2023-06-01 13:18:49 +02:00
[source,csharp,diff-id=1,diff-type=noncompliant]
----
int await = 42; // Noncompliant, but compiles
int async = 42; // Noncompliant, but compiles
----
2021-04-28 18:08:03 +02:00
2023-06-07 15:41:34 +02:00
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.
2023-06-01 13:18:49 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
2023-06-01 13:18:49 +02:00
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
2021-04-28 16:49:39 +02:00
----
2023-06-09 10:52:14 +02:00
To avoid any confusion, it is best to not use `async` and `await` as identifiers.
2021-04-28 18:08:03 +02:00
2023-06-01 13:18:49 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
2023-06-01 13:18:49 +02:00
int someVariableName = 42;
int someOtherVariableName = 42;
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-06-01 13:18:49 +02:00
== 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]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Rename "xxx" to not use a contextual keyword as an identifier.
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== 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"
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]