An identifier in C# is a name given to programming elements such as variables, classes, methods, namespaces, properties, interfaces, and objects. It is used to uniquely identify these elements in a program.
- Identifiers help in naming and accessing different elements in a C# program.
- Each identifier must follow C# naming rules to avoid compilation errors.
class Geeks
{
static void Main(string[] args)
{
int x = 9;
}
}
In the above program:
- Geeks -> identifier (class name)
- Main -> identifier (method name)
- args -> identifier (parameter name)
- x-> identifier (variable name)

Rules for Defining Identifiers
There are certain valid rules for defining a valid C# identifier. These rules should be followed, otherwise, we will get a compile-time error.
- The allowed characters for C# identifiers include letters (A-Z, a-z), digits (0-9), underscore (_), and Unicode characters. For example, geek@ is not a valid C# identifier because it contains @.
- Identifiers should not start with digits (0-9). For example, 123geeks is not a valid C# identifier.
- C# identifiers are case-sensitive. For example, student, Student, and STUDENT are treated as different identifiers.
- There is no fixed limit on the length of a C# identifier, but it is advisable to use meaningful and readable names.
- Spaces are not allowed in identifiers. For example, student name is invalid. Use studentName or student_name instead.
- C# keywords cannot normally be used as identifiers. For example, int class = 20; is invalid because class is a keyword.
- Keywords can be used as identifiers by prefixing them with @. For example, int @class = 20; is valid.
Note: C# has a set of reserved keywords and contextual keywords. Reserved keywords such as class, int, public, static, and void cannot normally be used as identifiers. The @ prefix can be used to escape a keyword and use it as an identifier.
Types of Identifiers
Identifiers in C# can represent different program elements:
- Class Identifiers: Names of classes (e.g., GFG)
- Method Identifiers: Names of methods (e.g., Main)
- Variable Identifiers: Names of variables (e.g., x)
- Object Identifiers: Names of objects created from classes
using System;
class Greeks
{
static void Main()
{
string name = "GFG";
Console.WriteLine(name);
}
}
Output
GFGIn the above example:
- Keywords: using, public, static, void.
- Identifiers: System, Main, Program.
Examples of Valid Identifiers
MyVariable
MYVARIABLE
myvariable
x
i
x1
i1
_myvariable
sum_of_array
student123
StudentName
totalMarks
Examples of Invalid Identifiers
My Variable // contains a space
123student // begins with a digit
a+c // contains +
variable-2 // contains -
sum&difference // contains &
class // keyword