What are identifiers in C#?

An identifier is a name used to identify a class, variable, function, or any other user-defined item in C#. Identifiers are fundamental building blocks that allow developers to name and reference different elements in their code.

Rules for Naming Identifiers

The basic rules for naming identifiers in C# are as follows −

  • A name must begin with a letter or underscore that could be followed by a sequence of letters, digits (0-9), or underscores. The first character in an identifier cannot be a digit.

  • It must not contain any embedded space or special symbols such as ? - + ! @ # % ^ & * ( ) [ ] { } . ; : " ' / \. However, an underscore (_) can be used.

  • It should not be a C# keyword or reserved word.

  • Identifiers are case-sensitive, so myVariable and MyVariable are different identifiers.

Syntax

Following is the general syntax for valid identifiers −

[letter|underscore][letter|digit|underscore]*

C# Identifier Rules Valid Identifiers myVariable _count student123 firstName MAX_SIZE Calculator Invalid Identifiers 123name my-variable class my variable @temp int

Examples of Valid Identifiers

Class Names

class Calculation
class StudentDetails
class MyProgram

Method Names

void Display()
void GetMarks()
int CalculateSum()

Variable Names

int age;
string firstName;
double averageMarks;
bool isValid;
int _counter;

Complete Example

using System;

class StudentRecord {
   public string studentName;
   public int studentAge;
   public double averageMarks;
   
   public void DisplayInfo() {
      Console.WriteLine("Student Name: " + studentName);
      Console.WriteLine("Age: " + studentAge);
      Console.WriteLine("Average Marks: " + averageMarks);
   }
   
   public bool IsEligible() {
      return averageMarks >= 60.0;
   }
}

class Program {
   public static void Main(string[] args) {
      StudentRecord student1 = new StudentRecord();
      student1.studentName = "John Doe";
      student1.studentAge = 20;
      student1.averageMarks = 85.5;
      
      student1.DisplayInfo();
      Console.WriteLine("Eligible for next level: " + student1.IsEligible());
   }
}

The output of the above code is −

Student Name: John Doe
Age: 20
Average Marks: 85.5
Eligible for next level: True

Naming Conventions

While the above rules define what makes an identifier valid, C# also follows certain naming conventions −

  • PascalCase for class names, method names, and properties: StudentRecord, DisplayInfo()

  • camelCase for variables and parameters: studentName, averageMarks

  • UPPER_CASE for constants: MAX_SIZE, PI_VALUE

Conclusion

Identifiers in C# are names used to identify various program elements like classes, methods, and variables. They must follow specific rules: start with a letter or underscore, contain only letters, digits, and underscores, and cannot be C# keywords. Following proper naming conventions makes code more readable and maintainable.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements