Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

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:

abstractasbasebool
breakbytecasecatch
charcheckedclassconst
continuedecimaldefaultdelegate
dodoubleelseenum
eventexplicitexternfalse
finallyfixedfloatfor
foreachgotoifimplicit
inin (generic modifier)intinterface
internalislocklong
namespacenewnullobject
operatoroutout (generic modifier)override
paramsprivateprotectedpublic
readonlyrefreturnsbyte
sealedshortsizeofstackalloc
staticstringstructswitch
thisthrowtruetry
typeofuintulongunchecked
unsafeushortusingusing static
voidvolatilewhile 

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.

addaliasascending
asyncawaitdescending
dynamicfromget
globalgroupinto
joinletorderby
partial (type)partial (method)remove
selectsetvalue
varwhen (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

Valid vs. invalid identifiers:

IdentifierRemark
numberValid
calculateMarksValid
hello$Invalid (contains $)
name1Valid
@ifValid (keyword with @)
ifInvalid (C# keyword)
My nameInvalid (whitespace)
_hello_hiValid

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:

KeywordIdentifier
usingSystem
namespaceHelloWorld
classHello
staticMain
voidargs
stringConsole
 WriteLine

The string literal "Hello World!" is passed to WriteLine.


C Language

  1. Mastering C++ Variables, Literals, and Constants: A Comprehensive Guide
  2. C Keywords & Identifiers: A Practical Guide
  3. Python Keywords and Identifiers: Mastering Reserved Words and Naming Conventions
  4. C Tokens, Identifiers, and Keywords: Understanding Tokens and Their Types
  5. Leveraging Robotics for Faster, Safer Aerospace & Defense Production
  6. Generative Design & Continuous 3D Fiber Deposition: AI-Driven Innovation
  7. Thermography Explained: How Infrared Imaging Detects Temperature Variations
  8. Revolutionizing Packaging: Advanced Robotics & Automation for Faster, Safer Shipments
  9. Navigating the Global Semiconductor Shortage: Impact and Solutions
  10. Carbon‑14 Dating: Unlocking Material Histories in Science & Industry