Programming languages ​​use identifiers for identification. In other words, an identifier is a user-defined name for a program component. In C#, an identifier is a class name, method name, variable name, or label.

public class test {
    static public void Main () 
    {
          int x;
    }
}

Here the total number of identifiers present in the above example is 3 and the names of these identifiers are:

  • Test Name of the class
  • Main: Method name
  • x: Variable name

Rules for defining identifiers in C#:

There are certain valid rules for defining a valid C# identifier. These rules should be followed, otherwise, we will get a compile-time error.  

  • The only allowed characters for identifiers are all alphanumeric characters([A-Z][a-z][0-9]), ‘_‘ (underscore). For example, “codeConfig@” is not a valid C# identifier as it contains ‘@’ – special character.
  • Identifiers should not start with digits ([0-9]). For example, “123user” is not valid in the C# identifier.
  • Identifiers should not contain white spaces.
     
  • Identifiers are not allowed to use as keywords unless they include @ as a prefix. For example, @as is a valid identifier, but “as” is not because it is a reserved keyword.
  • C# identifiers allow Unicode Characters.
  • C# identifiers are case-sensitive.
  • C# identifiers cannot contain more than 512 characters.
  • Identifiers do not contain two consecutive underscores in their name because such types of identifiers are used for the implementation.
class test {
 
    // Main Method
    static public void Main()
    {
 
        // variable
        int a = 10;
        int b = 20;
        int c;
 
        // simple addition
        c = a + b;
        Console.WriteLine("The sum of two number is: {0}", c);
    }
}
The sum of two number is: 30

Below table shows the identifiers and keywords present in the above example:

KeywordsIdentifiers
usingtest
publicMain
statica
voidb
intc

In this article we tried to explain C# | Identifiers with Examples. I hope you enjoyed reading this article. For more information you can check out Microsoft learn.

For more information you can visit our C# Section for more such articles.

Leave a Reply

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

Explore More

Working With File Class In C#

In C#, ‘File’ class provides many static methods for moving, copying, and deleting files. These Static methods involve moving a file and copying and deleting a file also. Let’s check

How to Read a Text File in C#

In C# ‘File’ class provides static methods to read given text file data. The File.ReadAllText() method opens a text file, reads all the text present in the file into a

C# | How to use Interface references

In C#, you’re permitted to make a reference variable of an interface type or in other words, you’re permitted to form an interface reference variable. Such kind of variable can