Curriculum
Keywords in C# are reserved words that have predefined meanings and cannot be used as identifiers (variable names, class names, method names, etc.) in the program. They are used to specify different aspects of the program’s structure, behavior, and control flow.
Examples of Keywords:
Here are a few examples of keywords in C#:
class MyClass { public int myPublicInt; private string myPrivateString; protected double myProtectedDouble; static float myStaticFloat; const decimal myConstantDecimal = 3.14m; volatile int myVolatileInt; override void MyMethod() { } sealed class MySealedClass { } abstract void MyAbstractMethod(); try { } catch (Exception ex) { } finally { } if (myPublicInt > 0) { } else if (myPublicInt < 0) { } else { } for (int i = 0; i < 10; i++) { } foreach (var item in myCollection) { } while (myPublicInt < 100) { } switch (myPublicInt) { case 0: break; default: break; } return; void MyVoidMethod() { } int MyIntMethod() { return 0; } string MyStringMethod() { return ""; } delegate int MyDelegate(int a, int b); async Task MyAsyncTask() { } using (var myResource = new MyResource()) { } lock (myLockObject) { } checked { } unchecked { } }
Rules:
Here are some rules to keep in mind when working with keywords in C#:
List of Keywords in C#:
Here is a list of all the keywords in C# with a quick overview of each one:
Keyword | Description |
---|---|
abstract | Used to define abstract classes and methods. |
as | Used to perform an explicit reference conversion. |
base | Used to access members of the base class. |
bool | Defines a Boolean data type. |
break | Used to exit a loop or switch statement. |
byte | Defines an 8-bit unsigned integer data type. |
case | Used in a switch statement to match a value. |
catch | Used to handle exceptions. |
char | Defines a character data type. |
checked | Used to enable overflow checking for integral-type arithmetic operations. |
class | Defines a class. |
const | Defines a constant value. |
continue | Used to skip to the next iteration of a loop. |
decimal | Defines a decimal data type. |
default | Used in a switch statement to define a default case. |
delegate | Used to define a delegate type. |
do | Used to start a do-while loop. |
double | Defines a double-precision floating-point data type. |
else | Used to define an alternative branch in an if statement. |
enum | Defines an enumerated data type. |
event | Used to define an event. |
explicit | Used to define explicit conversion operators. |
extern | Used to declare a method that is implemented externally. |
false | Represents the Boolean value false. |
finally | Used to define a block of code that is executed after a try block. |
fixed | Used |
Keyword | Description |
---|---|
float | Defines a single-precision floating-point data type. |
for | Used to define a for loop. |
foreach | Used to iterate over the elements of a collection. |
goto | Used to transfer control to another point in the program. |
if | Used to define a conditional statement. |
implicit | Used to define implicit conversion operators. |
in | Used in a foreach loop to specify the collection to iterate over. |
int | Defines a 32-bit signed integer data type. |
interface | Defines an interface. |
internal | Used to define a member that is accessible within the current assembly. |
is | Used to test an object for compatibility with a type. |
lock | Used to acquire a mutual-exclusion lock. |
long | Defines a 64-bit signed integer data type. |
namespace | Defines a namespace. |
new | Used to create a new instance of a class or value type. |
null | Represents a null reference. |
object | Defines a reference data type. |
operator | Used to define operator overloading methods. |
out | Used to pass a parameter by reference. |
override | Used to override a virtual or abstract member of a base class. |
params | Used to define a method that takes a variable number of arguments. |
private | Used to define a member that is accessible only within its declaring class. |
protected | Used to define a member that is accessible within its declaring class and its derived classes. |
public | Used to define a member that is accessible from any code. |
readonly | Used to define a field that can be set only in its declaration or constructor. |
ref | Used to pass a parameter by reference. |
return | Used to return a value from a method. |
sbyte | Defines an 8-bit signed integer data type. |
sealed | Used to prevent a class from being inherited. |
short | Defines a 16-bit signed integer data type. |
sizeof | Used to obtain the size of a value type. |
stackalloc | Used to allocate a block of memory on the stack. |
static | Used to define a member that belongs to the class itself, rather than to a specific instance. |
string | Defines a string data type. |
struct | Defines a value type. |
switch | Used to define a switch statement. |
this | Used to refer to the current instance of a class. |
throw | Used to raise an exception. |
true | Represents the Boolean value true. |
try | Used to define a block of code that might throw an exception. |
typeof | Used to obtain the System.Type object for a type. |
uint | Defines a 32-bit unsigned integer data type. |
ulong | Defines a 64-bit unsigned integer data type. |
unchecked | Used to disable overflow checking for integral-type arithmetic operations. |
unsafe | Used to define unsafe code that can access unmanaged memory. |
ushort | Defines a 16-bit unsigned integer data type. |
using | Used to define a namespace alias or to declare that a resource is no longer needed. |
virtual | Used to define a member that can be overridden in a derived class. |
void | Used to define a method that does not return a value. |
volatile | Used to indicate that a field can be modified by multiple threads. |
while | Used to define a while loop. |