C# Keywords and Identifiers

Every C# program uses two fundamental building blocks: keywords and identifiers. Understanding both helps you write clean, error-free code from the very beginning.

What Are Keywords?

Keywords are reserved words that C# already knows about. You cannot use them as names for your variables, classes, or methods. The C# compiler treats these words as special instructions.

Think of keywords like road signs. A stop sign always means "stop." You cannot rename a stop sign to something else — it has a fixed meaning for everyone on the road.

Common C# Keywords

Here is a visual map of keywords grouped by purpose:

┌─────────────────────────────────────────────────────────┐
│                   C# KEYWORDS MAP                       │
├─────────────────┬───────────────────────────────────────┤
│ Data Types      │ int, float, double, string, bool,     │
│                 │ char, decimal, long, byte, object     │
├─────────────────┼───────────────────────────────────────┤
│ Control Flow    │ if, else, switch, case, for, while,   │
│                 │ do, foreach, break, continue, return  │
├─────────────────┼───────────────────────────────────────┤
│ OOP             │ class, interface, new, this, base,    │
│                 │ abstract, sealed, override, virtual   │
├─────────────────┼───────────────────────────────────────┤
│ Access          │ public, private, protected, internal  │
├─────────────────┼───────────────────────────────────────┤
│ Other           │ using, namespace, try, catch, throw,  │
│                 │ finally, void, static, readonly       │
└─────────────────┴───────────────────────────────────────┘

Full List of C# Keywords

C# has around 79 reserved keywords. Some important ones you will use often:

abstract    as          base        bool        break
byte        case        catch       char        checked
class       const       continue    decimal     default
delegate    do          double      else        enum
event       explicit    extern      false       finally
fixed       float       for         foreach     goto
if          implicit    in          int         interface
internal    is          lock        long        namespace
new         null        object      operator    out
override    params      private     protected   public
readonly    ref         return      sbyte       sealed
short       sizeof      stackalloc  static      string
struct      switch      this        throw       true
try         typeof      uint        ulong       unchecked
unsafe      ushort      using       virtual     void
volatile    while

What Are Identifiers?

Identifiers are the names you create for things in your code — like variable names, method names, class names, and namespace names.

Think of identifiers like names on a mailbox. You choose the name, but you must follow certain rules so the postal system (the compiler) can find the right address.

Rules for Identifiers

┌─────────────────────────────────────────────────────────┐
│              IDENTIFIER RULES                           │
├───────────────────────┬─────────────────────────────────┤
│ Rule                  │ Example                         │
├───────────────────────┼─────────────────────────────────┤
│ Start with a letter   │ ✅ name, Score, _total          │
│ or underscore (_)     │ ❌ 1score, 9name                │
├───────────────────────┼─────────────────────────────────┤
│ Use letters, digits,  │ ✅ player1, my_score            │
│ underscores only      │ ❌ my-score, my score           │
├───────────────────────┼─────────────────────────────────┤
│ No spaces             │ ✅ totalScore                   │
│                       │ ❌ total Score                  │
├───────────────────────┼─────────────────────────────────┤
│ Cannot be a keyword   │ ✅ myClass                      │
│                       │ ❌ class                        │
├───────────────────────┼─────────────────────────────────┤
│ Case-sensitive        │ Score ≠ score ≠ SCORE           │
└───────────────────────┴─────────────────────────────────┘

Valid and Invalid Identifier Examples

// VALID identifiers
int age;
string firstName;
double _salary;
bool isLoggedIn;
int player1Score;

// INVALID identifiers
int 1name;        // starts with a digit
string my-name;   // hyphen not allowed
double int;       // 'int' is a keyword
bool is valid;    // space not allowed

Naming Conventions

C# developers follow standard naming styles. These are not enforced by the compiler, but they make code readable for everyone in a team.

Naming Convention Diagram

┌────────────────────┬────────────────────┬──────────────────────┐
│ Convention         │ Style              │ Used For             │
├────────────────────┼────────────────────┼──────────────────────┤
│ PascalCase         │ MyClassName        │ Classes, Methods     │
├────────────────────┼────────────────────┼──────────────────────┤
│ camelCase          │ myVariableName     │ Local variables,     │
│                    │                    │ parameters           │
├────────────────────┼────────────────────┼──────────────────────┤
│ _underscore        │ _privateField      │ Private fields       │
├────────────────────┼────────────────────┼──────────────────────┤
│ ALL_CAPS           │ MAX_SIZE           │ Constants (older     │
│                    │                    │ style)               │
└────────────────────┴────────────────────┴──────────────────────┘

Real Code Example

using System;

namespace MyFirstApp         // namespace identifier
{
    class StudentRecord      // class identifier (PascalCase)
    {
        string studentName;  // field identifier (camelCase)
        int age;             // field identifier

        void DisplayInfo()   // method identifier (PascalCase)
        {
            Console.WriteLine(studentName);
        }
    }
}

Contextual Keywords

C# also has contextual keywords. These words act as keywords only in specific situations. Outside those situations, you can use them as identifiers (though it is not recommended).

┌─────────────────────────────────────────────────────────┐
│            CONTEXTUAL KEYWORDS                          │
├──────────────────────────────────────────────────────── │
│ add        async       await       dynamic              │
│ get        global      nameof      partial              │
│ remove     set         value       var                  │
│ when       where       yield                            │
└─────────────────────────────────────────────────────────┘

For example, var is a contextual keyword. You can write var name = "Alice"; and the compiler figures out the type automatically. But var is not a reserved keyword — you could technically name a variable var, but that would confuse everyone reading your code.

Using the @ Symbol

If you absolutely must use a keyword as an identifier (very rare), prefix it with @. This tells the compiler to treat it as a name, not a keyword.

int @int = 5;       // legal but confusing — avoid this
string @class = "Math";  // legal but confusing

Only use @ when working with external libraries that use C# keywords as names. In your own code, always pick a different name instead.

Quick Reference Summary

┌───────────────────┬───────────────────────────────────────┐
│ Concept           │ Key Point                             │
├───────────────────┼───────────────────────────────────────┤
│ Keywords          │ Reserved — cannot use as names        │
│ Identifiers       │ Names you create — follow rules       │
│ Case sensitivity  │ Age ≠ age ≠ AGE                       │
│ PascalCase        │ For classes and methods               │
│ camelCase         │ For variables and parameters          │
│ @ prefix          │ Forces keyword to act as identifier   │
└───────────────────┴───────────────────────────────────────┘

Good naming makes code read almost like plain English. A variable named studentAge tells every reader exactly what it stores — no guessing needed.

Leave a Comment

Your email address will not be published. Required fields are marked *