Understanding C# Keywords and Identifiers: Rules, Lists, and Best Practices
C# Keywords and Identifiers
Master the fundamentals of C# keywords and identifiers, the building blocks that define your code’s structure and behavior.
C# Keywords
Keywords are predefined words that the C# compiler reserves for its own use. Their meaning is fixed; they cannot be altered, nor can they be used directly as identifiers.
For example:
long mobileNum;
Here, long is a keyword that declares a long‑typed variable named mobileNum. Keywords such as int, char, and long cannot be used as variable names without modification.
Attempts to use a keyword as an identifier, like long long;, will cause a compilation error. C# contains 79 reserved keywords, all in lowercase. Below is the complete list:
| 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 | in (generic modifier) | int | interface |
| internal | is | lock | long |
| namespace | new | null | object |
| operator | out | out (generic modifier) | 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 | using static |
| void | volatile | while |
Keywords can be used as identifiers when prefixed with @:
int @void;
This declares an int variable named @void.
Contextual Keywords
In addition to reserved keywords, C# defines 25 contextual keywords. These words acquire special meaning only in specific contexts and can otherwise serve as regular identifiers.
| add | alias | ascending |
| async | await | descending |
| dynamic | from | get |
| global | group | into |
| join | let | orderby |
| partial (type) | partial (method) | remove |
| select | set | value |
| var | when (filter condition) | where (generic type constraint) |
| yield |
For a deeper dive into each keyword, consult the official documentation: C# Keywords.
C# Identifiers
Identifiers give names to variables, methods, classes, and other program elements. They uniquely distinguish each element within a scope.
Example:
int value;
Here, value is an identifier. Attempting to use a reserved keyword without the @ prefix, such as int break;, will trigger a compile‑time error.
Learn more about variables in C# Variables.
Rules for Naming an Identifier
- It cannot be a C# keyword.
- It must begin with a letter, underscore (
_), or the@symbol; subsequent characters may be letters, digits, or underscores. - Whitespace and other symbols (e.g.,
$) are disallowed. - Identifiers are case‑sensitive: getName, GetName, and getname are distinct.
Valid vs. invalid identifiers:
| Identifier | Remark |
|---|---|
| number | Valid |
| calculateMarks | Valid |
| hello$ | Invalid (contains $) |
| name1 | Valid |
| @if | Valid (keyword with @) |
| if | Invalid (C# keyword) |
| My name | Invalid (whitespace) |
| _hello_hi | Valid |
Example: Extracting Keywords and Identifiers from a Program
Let’s examine a simple Hello World program to identify its keywords and identifiers.
using System;
namespace HelloWorld
{
class Hello
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Keyword–Identifier pairs in this snippet:
| Keyword | Identifier |
|---|---|
| using | System |
| namespace | HelloWorld |
| class | Hello |
| static | Main |
| void | args |
| string | Console |
| WriteLine |
The string literal "Hello World!" is passed to WriteLine.
C Language
- Mastering C++ Variables, Literals, and Constants: A Comprehensive Guide
- C Keywords & Identifiers: A Practical Guide
- Python Keywords and Identifiers: Mastering Reserved Words and Naming Conventions
- C Tokens, Identifiers, and Keywords: Understanding Tokens and Their Types
- Leveraging Robotics for Faster, Safer Aerospace & Defense Production
- Generative Design & Continuous 3D Fiber Deposition: AI-Driven Innovation
- Thermography Explained: How Infrared Imaging Detects Temperature Variations
- Revolutionizing Packaging: Advanced Robotics & Automation for Faster, Safer Shipments
- Navigating the Global Semiconductor Shortage: Impact and Solutions
- Carbon‑14 Dating: Unlocking Material Histories in Science & Industry