C# Keywords and Identifiers: In this tutorial, we will learn about watchwords (reserved words) and identifiers in C# programming language.

A keyword is a reserved word. You can’t use it as a variable name, constant name and so forth

In C# keywords can’t be used as identifiers. Be that as it may, on the off chance that we need to use the keywords as identifiers, we may prefix the keyword with @ character.

C# Keywords

Keywords are predefined sets of reserved words that have special meaning in a program. The significance of watchwords can not be changed, neither would they be able to be straightforwardly used as identifiers in a program.

For instance,

long mobileNum;

Here, long is a watchword and mobileNum is a variable (identifier). long has a special meaning in C# for example it is used to declare variables of type long and this function can’t be changed.

Likewise, watchwords like long, int, char, and so on can not be used as identifiers. Along these lines, we can’t have something like:

long long;

C# has a total of 79 watchwords. Every one of these catchphrases are in lowercase. Here is a finished rundown of all C# watchwords.

abstractasbasebool
breakbytecasecatch
charcheckedclassconst
continuedecimaldefaultdelegate
dodoubleelseenum
eventexplicitexternfalse
finallyfixedfloatfor
foreachgotoifimplicit
inin (generic modifier)intinterface
internalislocklong
namespacenewnullobject
operatoroutout (generic modifier)override
paramsprivateprotectedpublic
readonlyrefreturnsbyte
sealedshortsizeofstackalloc
staticstringstructswitch
thisthrowtruetry
typeofuintulongunchecked
unsafeushortusingusing static
voidvolatilewhile 

Although keywords are reserved words, they can be used as identifiers if @ is added as prefix. For instance,

int @void;

The above statement will create a variable @void of type int.


Contextual Keywords

Other than ordinary catchphrases, C# has 25 relevant watchwords. Contextual catchphrases have specific meaning in a limited program context and can be used as identifiers outside that unique circumstance. They are not reserved words in C#.

addaliasascending
asyncawaitdescending
dynamicfromget
globalgroupinto
joinletorderby
partial (type)partial (method)remove
selectsetvalue
varwhen (filter condition)where (generic type constraint)
yield  

On the off chance that you are interested to know the function of each watchword, I recommend you visit C# keywords (official C# docs).


C# Identifiers

Identifiers are the name given to entities like variables, methods, classes, and so on They are tokens in a program which uniquely recognize an element. For instance,

int value;

Here, value is the name of variable. Henceforth it is an identifier. Reserved catchphrases can not be used as identifiers unless if @ is added as prefix. For instance,

int break;

This statement will generate an error in compile time.


Rules for Naming an Identifier

• An identifier can not be a C# keyword.

• An identifier should start with a letter, an underscore or @ symbol. The excess piece of identifier can contain letters, digits and underscore symbol.

• Whitespaces are not allowed. Neither one of the its can have symbols other than letter, digits and underscore.

• Identifiers are case-touchy. In this way, getName, GetName and getname addresses 3 different identifiers.

Here are a portion of the legitimate and invalid identifiers:

IdentifiersRemarks
numberValid
calculateMarksValid
hello$Invalid (Contains $)
name1Valid
@ifValid (Keyword with prefix @)
ifInvalid (C# Keyword)
My nameInvalid (Contains whitespace)
_hello_hiValid

Example: Find list of keywords and identifiers in a program

Just to clear the idea, let’s discover the list of watchwords and identifiers in the program we wrote in C# Hello World.

using System;
namespace HelloWorld
{
    class Hello
{         
        static void Main(string[] args)
        {
          Console.WriteLine("Hello World!");
        }
    }
}
KeywordsIdentifiers
usingSystem
namespaceHelloWorld (namespace)
classHello (class)
staticMain (method)
voidargs
stringConsole
 WriteLine

The “Hello World!” inside WriteLine method is a string literal.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.


0 Comments

Leave a Reply

Avatar placeholder