What are tokens in C#?

A token is the smallest element of a C# program that the compiler can recognize. Tokens are the building blocks of C# code and include keywords, identifiers, literals, operators, and punctuation marks. Understanding tokens is fundamental to writing valid C# programs.

Types of Tokens in C# Keywords int, if, class Reserved words Identifiers myVar, Main Names Literals 42, "hello" Values Operators +, -, == Actions Symbols { } ; , Punctuation All tokens combine to form valid C# statements int myNumber = 42;

Keywords

Keywords are reserved words predefined by the C# compiler. These keywords have special meanings and cannot be used as identifiers. However, if you want to use these keywords as identifiers, you may prefix the keyword with the @ character.

Example of Using @ Prefix with Keywords

using System;

class Program {
   public static void Main() {
      int @int = 10;      // Using 'int' as variable name with @ prefix
      string @class = "MyClass";  // Using 'class' as variable name
      
      Console.WriteLine("Value of @int: " + @int);
      Console.WriteLine("Value of @class: " + @class);
   }
}

The output of the above code is −

Value of @int: 10
Value of @class: MyClass

The following are some of the reserved keywords in C# −

Type Keywords Control Keywords Access Keywords Modifier Keywords
bool, byte, char, int, float, double, string, object if, else, switch, case, for, foreach, while, do public, private, protected, internal abstract, static, virtual, override
decimal, long, short, uint, ulong, ushort break, continue, return, goto, try, catch, finally sealed, readonly, const async, await, partial, extern

Identifiers

An identifier is a name used to identify a class, variable, method, or any other user-defined item. Identifiers are tokens that you create to name your program elements.

Rules for Identifiers

  • Must begin with a letter (a-z, A-Z) or underscore (_), followed by letters, digits (0-9), or underscores.

  • The first character cannot be a digit.

  • Cannot contain spaces or special symbols like ? - + ! @ # % ^ & * ( ) [ ] { } . ; : " ' / \

  • Cannot be a reserved keyword (unless prefixed with @).

  • Are case-sensitive (MyVar and myvar are different).

Example of Valid and Invalid Identifiers

using System;

class Program {
   public static void Main() {
      // Valid identifiers
      int myNumber = 100;
      string firstName = "John";
      double _salary = 50000.50;
      bool isValid123 = true;
      
      // Display values
      Console.WriteLine("myNumber: " + myNumber);
      Console.WriteLine("firstName: " + firstName);
      Console.WriteLine("_salary: " + _salary);
      Console.WriteLine("isValid123: " + isValid123);
   }
}

The output of the above code is −

myNumber: 100
firstName: John
_salary: 50000.5
isValid123: True

Examples of Invalid Identifiers

int 2number;        // Cannot start with digit
string first-name;  // Cannot contain hyphen
double my salary;   // Cannot contain space
bool class;         // Cannot use keyword (unless @class)

Other Types of Tokens

Literals

Literals represent fixed values in the program −

using System;

class Program {
   public static void Main() {
      int intLiteral = 42;           // Integer literal
      double doubleLiteral = 3.14;   // Double literal
      string stringLiteral = "Hello"; // String literal
      char charLiteral = 'A';        // Character literal
      bool boolLiteral = true;       // Boolean literal
      
      Console.WriteLine("Integer: " + intLiteral);
      Console.WriteLine("Double: " + doubleLiteral);
      Console.WriteLine("String: " + stringLiteral);
      Console.WriteLine("Character: " + charLiteral);
      Console.WriteLine("Boolean: " + boolLiteral);
   }
}

The output of the above code is −

Integer: 42
Double: 3.14
String: Hello
Character: A
Boolean: True

Conclusion

Tokens are the fundamental building blocks of C# programs, consisting of keywords, identifiers, literals, operators, and symbols. Understanding token rules, especially for keywords and identifiers, is essential for writing syntactically correct C# code and avoiding compilation errors.

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

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements