C# Identifiers
An identifier is a user-defined name in C# that is used to uniquely identify an entity inside a program while it is running. Classes, variables, arrays, pointers, structures, interfaces, namespaces, functions (methods), and labels are a few examples of these entities. For instance, variables are designated memory spaces that hold values of a specific data type and are accessible within the program using their IDs. Code organization and name collision avoidance are facilitated by namespaces, which themselves offer a declarative region that gives scope to the identifiers within them.
Rules for Creating Identifiers
Guidelines for Identifying There are certain guidelines to follow while defining identifiers:
Allowed Characters: Decimal numbers (0–9), the underscore sign (_), and letters (uppercase A–Z and lowercase a–z) can all be included in an identification.
Starting Character: The first character must not be a number. It may instead begin with an underscore or a letter.
Special Symbols and Whitespace: There should be no special symbols (such as?, -, +,!, @, #, %, ^, &, *, (, ), [, ], {, },., ;, :, “, ‘, /, ) or whitespace between them in identifiers, save for the underscore symbol (_).
Keywords: You shouldn’t use C# keywords as identifiers. Keywords are reserved terms that have preset meanings for the C# compiler and cannot be altered.
Length: An identifier’s length is theoretically unlimited, yet the compiler may only take into account the first 31 characters.
Uniqueness: An identifier needs to be distinct throughout its range. It is against the law to use the same name twice in a routine, even in different code blocks.
Case-Sensitivity: The language C# is sensitive to case. As a result, capital and lowercase letters are handled differently; for instance, TOTAL is not the same as total or Total.
Recommendations for Better Programming
Better Programming Suggestions (Naming Conventions) In order to write code that is both legible and of good quality, certain naming practices are advised:
Meaningful Names: Meaningful and descriptive identifiers that express the purpose of the entity are essential. Asking “what value is stored in this variable” is the appropriate response. Do not use ambiguous names such as Problem12, garbage, temp, or K.
Avoid Starting with Underscore: In general, it is best to avoid using an underscore at the beginning of an identifier in order to avoid conflicts with system names.
Camel Casing for Variables/Parameters: Starting each identifier with a lowercase letter is advised. An identifier that has more than one word begins with a lowercase letter for the first word and an uppercase letter for the next (e.g., firstName, accountBalance). This is referred to as camel casing. Additionally, underscores can be used to divide words (e.g., first_Name).
Pascal Casing for Classes/Methods/Properties: Pascal Casing is advised for class names, method names, and property names. This means that the first letter of the identifier and the first letter of each concatenated word that follows are capitalised (e.g., BackColor, CreateReport).
Consistency: Throughout the code, keep the name consistent. In similar contexts, use the same terms, and name opposing ideas symmetrically.
Avoid Abbreviations: Since they might lead to misunderstandings, acronyms should typically be avoided unless they are widely recognised.
Boolean Identifiers: It is helpful to begin Boolean variables, property, or parameter names with is, has, or can (e.g., isValid, hasPermission, canRead).
Avoid Hungarian Notation: Hungarian notation, in which a prefix denotes the type or purpose of a variable, is often discouraged in contemporary C# programming since IDEs immediately offer type information.
Contextual Keywords and the @ Prefix
Some C# identifiers, like get and set in property accessors, have a unique meaning depending on the context; these are known as contextual keywords. Keywords can’t usually be used as user-defined names, but if you need to use a keyword as an identifier, you can prefix it with the @ character in C#.
Examples of Identifiers
Valid Identifiers:
int marks; // 'marks' is an identifier
char studentName; // 'studentName' is an identifier
float _a; // Starts with an underscore
double pi; // Simple lowercase identifier
string firstName; // Camel cased identifier
Invalid Identifiers:
int 123a; // Cannot start with a digit
int a b; // Contains whitespace
int break; // 'break' is a C# keyword
char str-; // Contains a special symbol other than underscore
Using nameof Operator for Identifiers
The nameof operator, which was first used in C# 6.0, returns a string containing the variable, type, or member’s unqualified name. For logging, errors, and user interface scenarios, this is assessed at compilation time, and IDEs can update the string if the identification is changed.
using System; // Required for Console.WriteLine
public class Program
{
public static void Main(string[] args)
{
int counter = 10;
Console.WriteLine(nameof(counter));
// You would call DoSomething like this:
Program p = new Program(); // Create an instance of the Program class
p.DoSomething(25); // Call the DoSomething method
}
public void DoSomething(int paramValue)
{
Console.WriteLine(nameof(paramValue));
}
}
Output
counter
paramValue
Identifiers in Expressions
Expressions, which are collections of operands (such as variables or literals) and operators that evaluate to a single value, are fundamentally identifiers.
The C# phrase if (income >= 10,000), for instance, designates income as a variable (an identifier).
You can also read What Are The Extension Methods In C# With Code Examples