Keywords in C# - GeeksforGeeks

Keywords in C#

Last Updated : 14 Oct, 2026

C# keywords are reserved words with predefined meanings used by the compiler to perform specific operations. They are part of the C# language syntax and generally cannot be used as identifiers unless the @ prefix is used.

  • Keywords cannot normally be used as identifiers such as variable, method, class, or object names.
  • C# also has contextual keywords, which have special meaning only in specific contexts.
  • Examples include int, class, if, return, public, etc.
C#
using System;

class Geeks {
    static void Main()
    {
        const int limit = 20;
        int marks = 25;

        if (marks >= limit) {
            Console.WriteLine("Passed");
        }
        else {
            Console.WriteLine("Failed");
        }
    }
}

Output
Passed

Explanation:

  • using imports a namespace.
  • class defines a class named Program.
  • static declares a method that belongs to the class itself.
  • void indicates that the method does not return a value.
  • const declares a constant.
  • int specifies an integer data type.
  • if and else control which block of code executes.

Types of Keywords

C# keywords are classified into two main categories based on how they are interpreted by the compiler.

frame_1000001866
Keywords in C#

1. Reserved Keywords

Reserved keywords have predefined meanings in C# and cannot normally be used as names for variables, classes, or methods.

Examples include int, class, if, while, return, and public.

C#
int age = 25;

if (age >= 18)
{
    Console.WriteLine("Eligible");
}

Here, int declares an integer variable, while if checks a condition.

2. Contextual Keywords

Contextual keywords have special meanings only in particular programming contexts. Outside those contexts, they can often be used as identifiers.

Examples include var, async, await, record, partial, and where.

var name = "GeeksforGeeks";
Console.WriteLine(name);

Here, var tells the compiler to infer the local variable's type from its initializer. In this case, the inferred type is string.

List of C# Keywords

C# keywords can be grouped according to their common purposes, such as declaring data types, controlling execution, handling exceptions, and defining classes.

The following sections organize the main reserved keywords into functional categories.

Data Type Keywords

Used to define variables and specify the kind of data they can hold.

KeywordUsage
boolRepresents a Boolean value, true or false.
byteRepresents an 8-bit unsigned integer.
sbyteRepresents an 8-bit signed integer.
charRepresents a 16-bit Unicode character.
shortRepresents a 16-bit signed integer.
ushortRepresents a 16-bit unsigned integer.
intRepresents a 32-bit signed integer.
uintRepresents a 32-bit unsigned integer.
longRepresents a 64-bit signed integer.
ulongRepresents a 64-bit unsigned integer.
floatRepresents a single-precision floating-point number.
doubleRepresents a double-precision floating-point number.
decimalRepresents a high-precision decimal number.
stringRepresents a sequence of characters.
objectRepresents the base type from which all C# types ultimately derive.
voidSpecifies that a method does not return a value.

Control Flow Keywords

Used to control the execution flow of a program.

KeywordUsage
ifExecutes code when a condition is true.
elseDefines an alternative block when an if condition is false.
switchSelects a block of code based on a value.
caseDefines an individual branch in a switch statement.
defaultDefines the block executed when no case matches.
forStarts a for loop.
foreachIterates through elements in a collection.
whileStarts a while loop.
doStarts a do-while loop.
breakTerminates the nearest loop or switch.
continueSkips to the next iteration of a loop.
gotoTransfers execution to a labeled statement.
returnExits from a method and optionally returns a value.

Exception Handling Keywords

Used for handling runtime errors and exceptions.

KeywordUsage
tryDefines a block of code in which exceptions may occur.
catchHandles an exception thrown by a try block.
finallyDefines code that executes after try/catch.
throwThrows an exception.

Object-Oriented Keywords

Used to define classes, objects, inheritance, interfaces, and related object-oriented features.

KeywordUsage
classDeclares a class.
interfaceDeclares an interface.
structDeclares a value-type structure.
enumDeclares a set of named constants.
delegateDefines a delegate type for referencing methods.
newCreates an object or invokes a constructor.
thisRefers to the current object.
baseRefers to members of the base class.
abstractDeclares a class or member that requires implementation by derived types.
sealedPrevents inheritance or further overriding.
virtualAllows a derived class to override a member.
overrideProvides a new implementation of an inherited virtual or abstract member.

Access Control Keywords

Define the accessibility of classes and their members.

KeywordUsage
publicAccessible from anywhere that can access the containing type.
privateAccessible only within the containing type.
protectedAccessible within the containing type and derived types.
internalAccessible within the current assembly.

Namespace and Using Keywords

Used to organize code and access types from namespaces.

KeywordUsage
namespaceDefines a scope for organizing related types and namespaces.
usingImports a namespace or defines other using directives.

C# namespaces organize related types and can also be declared using file-scoped syntax.

Modifier Keywords

Used to define additional characteristics of types and members.

KeywordUsage
staticIndicates that a member belongs to the type rather than an instance.
constDeclares a compile-time constant.
readonlyPrevents a field from being assigned except in permitted initialization contexts.
volatileIndicates that a field can be modified by multiple threads or external mechanisms.
externIndicates that a method is implemented externally.
unsafeEnables unsafe code operations.

Parameter and Reference Keywords

Used with method parameters and references.

KeywordUsage
refPasses a parameter by reference.
outPasses a parameter by reference for output.
inPasses a parameter by readonly reference.
paramsAllows a method to accept a variable number of arguments.

Type and Conversion Keywords

KeywordUsage
asPerforms a compatible reference, nullable, or boxing conversion.
isChecks whether an object is compatible with a specified type or pattern.
typeofObtains the System.Type object for a type.
sizeofObtains the size of a value type in bytes in permitted contexts.
defaultProduces the default value of a type or specifies a default case.

Operator Keywords

KeywordUsage
operatorDeclares an overloaded operator.
implicitDefines an implicit user-defined conversion.
explicitDefines an explicit user-defined conversion.

Memory Management and Synchronization Keywords

KeywordUsage
lockAcquires an exclusive lock for a critical section.
fixedPrevents the garbage collector from relocating a variable during an unsafe operation.
stackallocAllocates memory on the stack.

Checked and Unchecked Keywords

KeywordUsage
checkedEnables overflow checking for integral arithmetic operations.
uncheckedDisables overflow checking for integral arithmetic operations.

Contextual Keywords

Contextual keywords have special meaning only in particular contexts and may be used as ordinary identifiers outside those contexts.

KeywordUsage
varEnables implicitly typed local variables.
dynamicDefers type checking for certain operations until runtime.
asyncMarks a method, lambda, or local function as asynchronous.
awaitSuspends an asynchronous operation until the awaited task completes.
nameofProduces the name of a symbol as a string.
recordDeclares a record type.
requiredIndicates that a member must be initialized when an object is created.
partialAllows a type or member declaration to be split across multiple declarations.
getDefines the getter accessor of a property or indexer.
setDefines the setter accessor of a property or indexer.
initDefines an accessor that can assign a property during object initialization.
valueRepresents the value being assigned in a property or indexer setter.
yieldProduces values from an iterator.
withCreates a copy of an object with specified changes.
whenSpecifies a filter condition in certain contexts.
whereSpecifies generic constraints or filters query results.

LINQ Query Keywords

C# query expressions use contextual keywords such as from, where, select, group, orderby, join, let, into, on, equals, by, ascending, and descending.

KeywordUsage
fromSpecifies the data source of a query.
whereFilters elements based on a condition.
selectSpecifies the result of a query.
groupGroups query results.
orderbySorts query results.
joinCombines elements from two data sources.
letIntroduces a variable inside a query.
intoStores an intermediate query result.
onSpecifies the join condition.
equalsSpecifies equality between join keys.
bySpecifies a grouping key.
ascendingSorts query results in ascending order.
descendingSorts query results in descending order.

Special Literals

C# provides predefined literal values that represent special values.

LiteralUsage
trueRepresents the Boolean value true.
falseRepresents the Boolean value false.
nullRepresents a null reference or null value where applicable.

Keywords vs Identifiers

FeatureKeywordsIdentifiers
DefinitionReserved or context-specific words with predefined meanings in C#User-defined names for variables, methods, classes, etc.
PurposeDefine the syntax and structure of C# programsIdentify program elements
Can be ModifiedNoYes
Usage as Variable NameNot normally allowedAllowed
Predefined by C#YesNo
Examplesint, class, if, returnstudentName, calculateSum, Employee
Case SensitiveYesYes
Compiler MeaningDefined by the C# languageDefined by the programmer

Important: Unlike Java's final, C# uses const, readonly, and other mechanisms for different forms of immutability; and unlike Java, C# has the @ escape mechanism for using keywords as identifiers.

Comment

Explore