In programming, keywords are reserved words for your compiler. Their meaning are already defined in compiler and are used for specific purpose only. These keywords cannot be used as identifiers(i.e, cannot be used for naming variables, classes etc.), however in C#, you can use keywords as variable or class name by using prefix “@”.
For example: break is a keyword not an identifier. But if you want to use break as an identifier, you should use @break.
List of C# Keywords
Keyword | Description of Keyword |
---|---|
abstract | used to design base class |
as | used for type conversion |
base | used to access members of the base class from a derived class |
bool | used to declare variables to store the boolean values (true and false) |
break | terminates a loop |
byte | denotes an integral type that stores values ranging from 0 to 255 and is unsigned 8-bit integer |
case | is a part of switch keyword which is used to match values in switch |
catch | specify handlers for different exceptions |
char | used to declare a unicode character ranging from U+0000 to U+FFFF and is 16-bit character |
checked | used for arithmetic overflow checking |
class | used to declare class |
const | used to make an entity unchangeable during program runtime |
continue | passes control to the next iteration |
decimal | denotes a 128-bit data type |
default | returns default value |
delegate | used to declare a programming construct to create callable reference |
do | loops a block of statements until a specified expression is false |
double | denotes a simple type that stores 64-bit floating point values |
else | selects a statement for execution based on the value of a boolean expression |
enum | used to declare an enumeration |
event | used to declare an event |
explicit | used to declare an explicit user-defined type conversion operator |
extern | used to indicate that the method is implemented externally |
false | used to indicate that an operand is false or represent the boolean value false |
finally | makes sure to execute block of statements before the method is exited |
fixed | prevents reallocation of memory |
float | denotes a data type that stores 32-bit floating-point values |
for | loops a block of statements until a specified expression is false |
foreach | repeats a group of statements for each element in an array |
goto | transfers the program control to a labeled statement |
if | selects a statement for execution based on the value of a boolean expression |
implicit | used to declare an implicit user-defined type conversion operator |
in | in foreach statement, in statement repeats a group of statements for each element in an array |
int | denotes an integral type that stores values that ranges from -2,147,483,648 to 2,147,483,647 and is signed 32-bit integer |
interface | is a contract that all the classes inheriting the interface should follow |
internal | is an access modifier for types and type members and they are accessible only within files in the same assembly |
is | checks if an object is compatible with required type |
lock | allows only one thread to execute at a time |
long | denotes an integral type that stores values that ranges from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and is signed 64-bit integer |
namespace | used to organize related set of objects or codes |
new | used to create objects and invoke constructors or hide an inherited member from a base class member or restrict types that might be used as arguments for a type parameter in a generic declaration |
null | represents a null reference |
object | base class for all derived classes and provides some methods and capabilities |
operator | used to declare an operator |
out | allows to return more than one value without storing class state |
override | used to modify the abstract or virtual implementation a method, a property, an indexer, or an event |
params | allows methods to receive variable numbers of parameters |
private | allows access of the member only by the members of that class or struct |
protected | allows access of the member within its class and by derived class instances |
public | allows access of the member from anywhere |
readonly | prevents changes to fields after initializing |
ref | causes a method to refer to the same variable that was passed into the method |
return | terminates execution of the method returning control to the calling method and can also return a value |
sbyte | denotes an integral type that stores values ranging from -128 to 127 and is signed 8-bit integer |
sealed | makes a class inheritable |
short | denotes an integral data type that stores values ranging from -32,768 to 32,767 and is signed 16-bit integer |
sizeof | obtains the size of a type |
stackalloc | used to allocate a block of memory on the stack |
static | used to declare a static member |
string | represents unicode characters |
struct | used to put small groups of related variables together |
switch | allows a variables to be compared with different cases |
this | refers to the current instance of the class |
throw | when a problem occurs, it throws an exception |
true | used to indicate that an operand is true or represent the boolean value true |
try | begins an exception handling block |
typeof | obtains the System.Type object for a type and can be overloaded |
unit | denotes an integral type that stores values ranging from 0 to 4,294,967,295 and is unsigned 32-bit integer |
ulong | denotes an integral type that stores values ranging from 0 to 18,446,744,073,709,551,615 and is unsigned 64-bit integer |
unchecked | used for arithmetic overflow checking |
unsafe | denotes an unsafe context |
ushort | denotes an integral data type that stores values ranging from 0 to 65,535 and is unsigned 16-bit integer |
using | used to import types defined in other namespaces or define a scope at the end of which an object will be disposed |
virtual | used to modify a method or property declaration and allow for it to be overridden in a derived class |
void | used as return type for a method and also specifies that the method does not return a value |
volatile | indicates that a field might be modified by multiple threads that are executing at the same time |
while | executes a statement or a block of statements until a specified expression evaluates to false |
Contextual Keywords
There are contextual keywords in C# as well and are used to provide a specific meaning in the code. They are not reserved word in C# like reserved keywords. Some of them are listed below:
Keyword | Description of Keyword |
---|---|
add | used to create a event accessor |
async | used to specify that a method is asynchronous |
await | suspends the execution of the asynchronous method until the waiting task is completed |
dynamic | enables an operation to be resolved in run time instead of compile time checking |
get | used to define an accessor method in a proper and get its value |
global | specifies default global namespace |
partial | splits class or struct into multiple files |
remove | used to get out of an event |
set | used to define an accessor method in a proper and set its value |
value | sets accessor and add or remove event handlers |
var | initializes variable that is aliased type and is determined by the C# compiler |
where | used to specify constraints |
yield | used to return a value to the enumerator object or to signal the end of iteration |
Depending upon the purpose of keywords, keywords in C# can be vaguely categorized as:
- Operator keywords: In C#, some keywords can be used as an operator. For example: sizeof is an operator to find size of a data type. Some operator keywords in C# are stackalloc, typeof etc.
- Statement keywords: In C#, some keywords can be used to control the sequence of statement execution. For example: break operator is used to terminate a loop. Some statement keywords in C# are if, else, do, foreach, goto etc.
- Namespace keywords: In C#, these keywords are used to define namespaces. Some namespace keywords are using, namespace etc.
- Conversion keywords: In C#, some keywords are used for type conversion. For example: explicit keyword is used to perform user defined conversion. Some other conversion keywords are implicit, operator etc.
- Access keywords: In C#, some keywords can be used to access other members of a class. Some example of access keywords are this, base etc.
- Literal keywords: In C#, literal is used for representing a fixed value in a program.
For example: var context = true;
Here, true is a literal. Some example of literal keywords are default, null, false etc.