Mastering C# Identifiers
The Complete Guide to Compiler Rules, Naming Standards, and Framework Conventions
When writing C# code, how you name your program elements—classes, variables, methods, and namespaces—matters immensely. These names are called identifiers. In the .NET ecosystem, identifiers are governed by two separate sets of guidelines: strict Compiler Rules (the absolute laws that dictate whether your code builds) and Framework Design Guidelines (the industry standards that make your code readable and maintainable). Let's break down both.
1. Strict Compiler Rules (The "Law")
If you break these rules, the C# compiler will flag an error, and your application will fail to build. Treat these as absolute boundaries:
- Allowed Characters: Identifiers can contain alphanumeric characters (letters and numbers) and the underscore symbol (
_). Full Unicode character sets are supported. - First Character Limit: An identifier must begin with a letter or an underscore. It cannot begin with a number.
- Case Sensitivity: C# is strictly case-sensitive. This means
myVariable,MyVariable, andMYVARIABLEare treated as three completely unique identifiers. - No Spaces or Symbols: You cannot include whitespace, hyphens, periods, or special characters inside an identifier name.
- Keyword Restrictions: You cannot use C# reserved keywords (such as
class,int, orpublic) directly. However, you can bypass this by prefixing the keyword with the@character (known as a verbatim identifier).
int @class = 5; // Completely legal, though discouraged for general use
2. Standard Naming Conventions (The "Style Guide")
While the compiler ensures your code runs, standard conventions ensure humans can read it. .NET relies heavily on two casing styles:
- PascalCase: Capitalize the first letter of each word (e.g.,
TotalPrice). - camelCase: Capitalize the first letter of each word except the first word (e.g.,
totalPrice).
| Element Type | Casing Style | Rules & Best Practices | Example |
|---|---|---|---|
| Class, Struct, Record | PascalCase |
Use a noun or noun phrase. | ShoppingCart |
| Interface | PascalCase |
Always start with a capital I. Use an adjective/noun. | IOrderProcessor |
| Method | PascalCase |
Always start with a verb or verb phrase. | CalculateTotal() |
| Property | PascalCase |
Use a descriptive noun or adjective phrase. | IsCompleted |
| Namespace | PascalCase |
Follows a CompanyName.Technology pattern. |
System.Text |
| Local Variables | camelCase |
Keep names descriptive but reasonably concise. | calculatedTotal |
| Method Parameters | camelCase |
Match the incoming data profile cleanly. | username |
3. Advanced Scenarios and Special Rules
Private Class-Level Fields (_camelCase)
For private backing fields inside classes, developers use a leading underscore followed by standard camelCase. This makes it instantly obvious that a variable belongs to class-wide scope rather than local method scopes.
Constants (PascalCase)
Unlike Java or C++ which leverage macro-style ALL_CAPS_WITH_UNDERSCORES, C# treats constants (const) and static read-only fields using standard PascalCase.
Generics Type Parameters (T)
Single generic type parameters use a lone uppercase capital T. If multiple generic parameters are required, prefix every descriptive word with a capital T (e.g., TKey, TValue).
public class UserService { private readonly ILogger _logger; // Private class field public const int MaxRetries = 3; // Constant public void ProcessLogin(string username) { var currentTimestamp = DateTime.UtcNow; // Local variable } }
💡 Quick Best Practices to Keep in Mind
- Avoid Abbreviations: Favor absolute readability over shortcut names. Use
SystemConfigurationinstead ofSysConfig. - Drop Hungarian Notation: Do not prefix names with data types (use
int age;instead of legacy patterns likeint iAge;). - Boolean Clarity: Name boolean variables and properties to clearly imply a true/false condition, using helpful prefixes like
Is,Has, orCan(e.g.,IsEnabled).
Comments
Post a Comment