What are Identifiers and Keywords?
Identifiers are the names given to classes, methods, variables and interfaces. It must be a whole word and starts with either an alphabet or an underscore. They are case sensitive. The main point you should bear in mind is that the names should not clash with C# keywords. Some programmers use @ prefix as a first character while declaring identifiers to avoid clash with a keyword but it is not a recommended practice. Following names are valid identifiers in C#
1. Hello
2. hello
3. H_ello
4. HelloWorld
You should name the variables using the standard DataType prefixes. Also the first alphabet after the prefix should be capitalized. Table 1 shows a list of prefixes for the various .NET DataTypes. Interfaces should be named with "I" as the first alphabet so that you can easily distinguish it from a class.
Table 1
|
Data Type |
Prefix |
Example |
|
Array |
arr |
arrNumber |
|
Boolean |
bln |
blnSelect |
|
Byte |
byt |
bytNumber |
|
Char |
chr |
chrPick |
|
DateTime |
dtm |
dtmPick |
|
Decimal |
dec |
decPoint |
|
Double |
dbl |
dblData |
|
Integer |
int |
intVar |
|
Long |
lng |
lngMiles |
|
Object |
obj |
objVar |
|
Short |
shr |
shrNumber |
|
Single |
sng |
sngNumber |
|
String |
str |
strAddress |
All Windows Forms controls should be named with the special prefixes as shown in Table 2. This is to avoid confusion and also to distinguish between other controls in a complex project. As explained above, the first alphabet after the prefix should be capitalized. Once you master the naming conventions and prefixes it would be very easy for you to write and maintain code.
Table 2
|
Control Name |
Prefix |
Example |
|
Button |
btn |
btnSubmit |
|
TextBox |
txt |
txtFname |
|
CheckBox |
chk |
chkHobbies |
|
RadioButton |
rad |
radMale |
|
Image |
img |
imgIndia |
|
Label |
lbl |
lblCity |
|
Calendar |
cal |
calDate |
It is beyond the scope of this chapter to cover the prefixes of all the .NET controls. You will find a detailed list of them on the MSDN Library. Keywords are special words built into the C# language and are reserved for specific use. This means you cannot use them for naming your classes, methods and variables. For instance, if you attempt to use a C# keyword (if) as your class name, the C# compiler will emit runtime error as shown in Figure 1.
In order to resolve the problem, you may have to change the class name to some other meaningful name. There are around 80 keywords in C#, which can be used by programmers to develop applications using the user friendly language.
Categories: C#, Channels, Latest Tags: .net framework, C#, identifiers, keywords, microsoft