Comprehensive List of C# and .NET Keywords and Their Usages
C# has a rich set of keywords that are used to define the structure and behavior of the code. Here’s a comprehensive list of C# and .NET keywords, along with their explanations and possible usages.
Access Modifiers:
public: Access is not restricted.
public class MyClass { }
private: Access is limited to the containing type.
private int myField;
protected: Access is limited to the containing class and derived classes.
protected void MyMethod() { }
internal: Access is limited to the current assembly.
internal class InternalClass { }
protected internal: Access is limited to the current assembly or types derived from the containing class.
protected internal void ProtectedInternalMethod() { }
private protected: Access is limited to the containing class or types derived from the containing class within the same assembly.
private protected void PrivateProtectedMethod() { }
Modifiers:
abstract: Indicates that a class or a member is incomplete and must be implemented in a derived class.
public abstract class AbstractClass {
public abstract void AbstractMethod();
}
async: Used to specify that a method is asynchronous.
public async Task MyAsyncMethod() {
await Task.Delay(1000);
}
const: Indicates that the value of the field or local variable cannot be modified.
const int MyConstant = 10;
event: Declares an event.
public event EventHandler MyEvent;
extern: Indicates that the method is implemented externally.
[DllImport("user32.dll")]
private static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
override: Indicates that a method is overriding a method in a base class.
public override string ToString() {
return "Overridden method";
}
readonly: Indicates that a field can only be assigned values during its declaration or in the constructor.
public readonly int ReadOnlyField = 10;
sealed: Prevents a class from being inherited.
public sealed class SealedClass { }
static: Declares a static member that belongs to the type itself rather than to a specific object.
public static int StaticField;
unsafe: Declares an unsafe context in which pointers can be used.
unsafe {
int* p = &myInt;
}
virtual: Indicates that a method or property can be overridden in a derived class.
public virtual void VirtualMethod() { }
volatile: Indicates that a field might be modified by multiple threads that are executing at the same time.
private volatile int myVolatileField;
Type Keywords:
bool: Boolean type.
bool isTrue = true;
byte: 8-bit unsigned integer.
byte myByte = 255;
char: 16-bit Unicode character.
char myChar = 'A';
decimal: 128-bit precise decimal values with 28-29 significant digits.
decimal myDecimal = 100.5m;
double: 64-bit double-precision floating point.
double myDouble = 99.99;
float: 32-bit single-precision floating point.
float myFloat = 99.99f;
int: 32-bit signed integer.
int myInt = 1000;
long: 64-bit signed integer.
long myLong = 100000L;
object: Base type for all other types.
object myObject = new Object();
sbyte: 8-bit signed integer.
sbyte mySByte = -128;
short: 16-bit signed integer.
short myShort = 100;
string: Represents text as a series of Unicode characters.
string myString = "Hello, World!";
uint: 32-bit unsigned integer.
uint myUInt = 1000u;
ulong: 64-bit unsigned integer.
ulong myULong = 100000UL;
ushort: 16-bit unsigned integer.
ushort myUShort = 100;
Control Flow Keywords:
if, else: Conditional branching.
if (condition) {
// code
} else {
// code
}
switch, case: Multi-way branching.
switch (variable) {
case 1:
// code
break;
default:
// code
break;
}
for: Loop with a counter.
for (int i = 0; i < 10; i++) {
// code
}
foreach: Loop through each item in a collection.
foreach (var item in collection) {
// code
}
while: Loop with a condition.
while (condition) {
// code
}
do: Loop with a condition at the end.
do {
// code
} while (condition);
break: Exit a loop or switch statement.
break;
continue: Skip the current iteration of a loop.
continue;
return: Exit a method and optionally return a value.
return value;
yield: Provide values to an iterator.
yield return value;
Exception Handling Keywords:
try, catch, finally: Exception handling.
try {
// code
} catch (Exception ex) {
// handle exception
} finally {
// cleanup code
}
throw: Throw an exception.
throw new Exception("Error message");
Other Keywords:
as: Type conversion.
object obj = "Hello, World!";
string str = obj as string;
is: Type checking.
if (obj is string) {
// code
}
new: Create a new instance of a type.
MyClass obj = new MyClass();
sizeof: Get the size of a type.
int size = sizeof(int);
typeof: Get the Type object for a type.
Type type = typeof(int);
checked: Enable overflow checking for arithmetic operations.
int result = checked(2147483647 + 1);
unchecked: Disable overflow checking for arithmetic operations.
int result = unchecked(2147483647 + 1);
lock: Ensure that one thread does not enter a critical section while another thread is in the critical section.
lock (obj) {
// code
}
using: Ensure the correct use of IDisposable objects.
using (var resource = new Resource()) {
// code
}
namespace: Declare a scope that contains a set of related objects.
namespace MyNamespace {
class MyClass { }
}
class, struct, interface, enum: Type definitions.
class MyClass { }
struct MyStruct { }
interface IMyInterface { }
enum MyEnum { Value1, Value2 }
delegate: Declare a delegate type.
public delegate void MyDelegate(string message);
base: Access members of the base class from within a derived class.
base.Method();
this: Access members of the current instance.
this.Field = value;
in, ref, out: Parameter modifiers.
void MyMethod(in int x, ref int y, out int z) {
z = 0;
}
var: Implicitly typed local variable.
var myVar = 10;
dynamic: Bypass compile-time type checking.
dynamic obj = "Hello, World!";
default: Get the default value of a type.
int defaultValue = default(int);
params: Parameter array.
void MyMethod(params int[] numbers) { }
value: Used in property setters.
private int myValue;
public int MyProperty {
set { myValue = value; }
}
where: Specify constraints on generics.
void MyMethod<T>(T param) where T : class { }
nameof: Get the name of a variable, type, or member.
string name = nameof(MyMethod);
await: Suspend the execution of an async method until the awaited task completes.
await Task.Delay(1000);