C# | Identifiers

In programming languages, identifiers are used for identification purpose. Or in other words, identifiers are the user-defined name of the program components. In C#, an identifier can be a class name, method name, variable name or a label.

Example:

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

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



  • GFG: 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 “geek@” is not a valid C# identifier as it contain ‘@’ – special character.
  • Identifiers should not start with digits([0-9]). For example “123geeks” is a not a valid in C# identifier.
  • Identifiers should not contain white spaces.
  • Identifiers are not allowed to use as keyword unless they include @ as a prefix. For example, @as is a valid identifier, but “as” is not because it is a keyword.
  • C# identifers allow Unicode Characters.
  • C# identifiers are case-sensitive.
  • C# identifers cannot contain more than 512 characters.
  • Identifiers does not contain two consecutive underscores in its name because such types of identifiers are used for the implementation.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple C# program to illustrate identifiers
using System;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // variable
        int a = 10;
        int b = 39;
        int c;
  
        // simple addition
        c = a + b;
        Console.WriteLine("The sum of two number is: {0}", c);
    }
}

chevron_right


Output:

The sum of two number is: 49

Below table shows the identifers and keywrods present in the above example:

Keywords Identifiers
using GFG
public Main
static a
void b
int c


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : onidavid97, shubham_singh



Article Tags :

4


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.