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.
In this article, you will learn-
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.
abstract | as | base | bool |
break | byte | case | catch |
char | checked | class | const |
continue | decimal | default | delegate |
do | double | else | enum |
event | explicit | extern | false |
finally | fixed | float | for |
foreach | goto | if | implicit |
in | in (generic modifier) | int | interface |
internal | is | lock | long |
namespace | new | null | object |
operator | out | out (generic modifier) | override |
params | private | protected | public |
readonly | ref | return | sbyte |
sealed | short | sizeof | stackalloc |
static | string | struct | switch |
this | throw | true | try |
typeof | uint | ulong | unchecked |
unsafe | ushort | using | using static |
void | volatile | while |
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#.
add | alias | ascending |
async | await | descending |
dynamic | from | get |
global | group | into |
join | let | orderby |
partial (type) | partial (method) | remove |
select | set | value |
var | when (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:
Identifiers | Remarks |
number | Valid |
calculateMarks | Valid |
hello$ | Invalid (Contains $) |
name1 | Valid |
@if | Valid (Keyword with prefix @) |
if | Invalid (C# Keyword) |
My name | Invalid (Contains whitespace) |
_hello_hi | Valid |
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!"); } } }
Keywords | Identifiers |
using | System |
namespace | HelloWorld (namespace) |
class | Hello (class) |
static | Main (method) |
void | args |
string | Console |
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.