C# keywords
In C#, keywords are reserved words that have a special meaning and cannot be used as identifiers (such as variable or method names). if we want to use the keywords as identifiers, we may prefix the keyword with @ character.
Example
@if is a valid identifier, but if isn't because if is a keyword.
public class @class
{
public static int salary { get; set; }
}
@class.salary = 100000;
The following are the keywords in C# language to define the syntax and structure.
abstract | base | as | bool | break | catch | case |
byte | char | checked | class | const | continue | decimal |
default | delegate | do | double | else | enum | event |
sealed | short | sizeof | stackalloc | static | string | struct |
switch | this | throw | true | try | typeof | uint |
private | protected | public | return | readonly | ref | sbyte |
explicit | extern | false | finally | fixed | float | for |
foreach | goto | if | implicit | in | in (generic modifier) | int |
ulong | ushort | unchecked | using | unsafe | virtual | void |
null | object | operator | out | out (generic modifier) | override | params |
abstract | base | as | bool | break | catch | case |
volatile | while |
A contextual keyword is used to provide a specific meaning in the code, but it isn't a reserved word in C#. Some contextual keywords, such as partial and where, have special meanings in two or more contexts.
public class @class
{
public static int var{ get; set; }
public static int var{ get; set; }
}
The above example var is a contextual keyword. This is a reserver keyword.
The following are the some contextual keywords in C# language.
var | group | ascending | descending | dynamic | from | get |
global | alias | into | join | let | select | set |
scoped | nameof | remove | join | record | remove | required |
notnull | nuint | on | or | orderby | partial (type) | partial (method) |