![]() |
VOOZH | about |
In C#, identifiers are the user-defined names given to program elements such as variables, methods, classes, and labels. They are used to identify these elements in a program. Example:
In the above code block- "x", "GFG" and "Main" are identifiers representing a variable, class, and method, respectively and are user-defined names used to uniquely identify program elements in C#.
There are certain valid rules for defining a valid C# identifier. These rules should be followed, otherwise, we will get a compile-time error.
Aspect | Description |
|---|---|
Allowed Characters | The only allowed characters for identifiers are all alphanumeric characters([A-Z], [a-z], [0-9]), '_' (underscore). |
Starting Character | Identifiers should not start with digits([0-9]). For example, "123geeks" is not valid in the C# identifier. |
No Whitespaces | Identifiers must not contain whitespace characters. |
Keywords | Identifiers are not allowed to use as keywords unless they include @ as a prefix. For example, @as is a valid identifier, but "as" is not because it is a keyword. |
Unicode Support | C# identifiers allow Unicode Characters. |
Case - Sensitivity | C# identifiers are case-sensitive. |
Length Restriction | There is no strict length limit defined for identifiers in C#, but they should be kept short and meaningful for readability. |
Underscore Usage | Identifiers can contain underscores, but using consecutive underscores (e.g., __name) is discouraged as such patterns are typically reserved for internal or compiler-generated use. |
Identifiers in C# can represent different program elements:
The sum of two numbers is: 49
In the above example:
Use meaningful names (e.g., totalMarks instead of tm). Use the following naming conventions:
Avoid using reserved keywords as identifiers.