Identifiers

An identifier is a string used to name program elements such as variables, methods and parameters. It’s recommended to combine semantically meaningful English words to write self-documenting code (e.g. PlayersHand, SocialSecurityNum).

Simply put, identifiers are the names assigned to variables, methods, properties and other elements. These names can’t be created arbitrarily and must follow specific rules, much like how people’s legal names have to comply with naming conventions.

Valid Character Rules for Identifiers

PositionAllowed CharactersForbidden CharactersRemarks
First CharacterLowercase a-z, uppercase A-Z, underscore _, @Numerals 0-9@ can only be placed at the start and is not advised for regular development work
Subsequent CharactersLowercase a-z, uppercase A-Z, underscore _, numerals 0-9@@ cannot appear in the middle or at the end of an identifier

Valid Identifiers

userName
PlayerScore
_totalMoney
Age
StudentID
maxHP
OrderList
isLogin
_apple
_123Rate
_UserInfo
@int
@class
@string
@whileCode language: JavaScript (javascript)

Invalid Identifiers

Numeral as first character
99Game
123Name

@ symbol in middle / end position
0user
user@Name
score@
my@var

The following are reserved C# keywords used directly without escaping
int
class
static
void

Contains invalid special symbols
user name
student#id
money&numCode language: PHP (php)

Case Sensitivity

Identifiers are case-sensitive. Examples shown below:

int totalMovieCount;
int TotalMovieCount;
int TotalmovieCount;

These three count as completely separate variables. While syntactically legal, such naming is highly confusing and should be avoided.

Code Example

// See https://aka.ms/new-console-template for more information
using System;

Console.WriteLine("Hello, World!");

int totalMovieCount;
int TotalMovieCount;
int TotalmovieCount;


// ========== 1. Invalid Identifiers (Trigger Compile Errors) ==========
// 1. Starts with a number
int 99Game;
string 123Name;
double 0user;

// 2. Contains @ in middle or trailing position
int user@Name;
float score@;
bool my@var;

// 3. Raw C# reserved keywords without @ escape
int int;
string class;
double static;
void void;

// 4. Illegal special characters including space, #, &
int user name;
string student#id;
decimal money#

// ========== 2. Valid Identifiers (Compile Without Errors) ==========
// Standard recommended naming (no @, clean & readable)
int userName;
int PlayerScore;
float _totalMoney;
bool isLogin;
string StudentID;

// Syntactically legal but not recommended (@ escaped keywords, reserved for edge cases only)
int @int;
string @class;
double @static;
void @void;
Code language: HTML, XML (xml)

Leave a Reply

Your email address will not be published. Required fields are marked *