Identifiers and Naming Rules

Identifiers are the names used for variables, methods, classes, and other entities in C#. Identifiers must follow certain naming rules to be valid. They must start with a letter or underscore and cannot be a reserved keyword.

Key Concepts

  • Identifiers must begin with a letter or underscore.
  • They can contain letters, numbers, and underscores, but cannot include spaces or special characters.
  • Identifiers cannot be C# reserved keywords, such as class or int.

Example of Valid and Invalid Identifiers

Code Example

// Valid identifiers
int myVariable = 10;
string _name = "Alice";

// Invalid identifiers (commented out)
// int 1stNumber = 5; // Cannot start with a number
// string class = "Invalid"; // Cannot use reserved keywords

Output:

myVariable = 10 _name = Alice

Code Explanation: The valid identifiers myVariable and _name follow the rules of naming in C#. Invalid identifiers such as 1stNumber and class (a reserved keyword) are commented out to demonstrate incorrect usage.