C# FAQ 1.6 - What are Identifiers and Keywords?

Identifiers

Identifiers are names given to namespaces, classes, methods, variables, and interfaces. An identifier must be a whole word and start with either a letter or an underscore. It can be any combination of letters, numbers, and underscores. But, it should not start with a number. Prior to .NET, programmers were using Hungarian-notation guidelines for naming identifiers; however, with .NET Microsoft has recommended Pascal and Camel notations for identifiers. They have also suggested using semantics in the identifier name. Another point you should bear in mind is that identifiers should not be the same as a C# keyword as listed in the section "Keywords." For example, the following code is illegal:

// Error. int cannot be used as a variable name as it a keyword
int int = 5;

Identifiers in C# are case sensitive. For instance, X is not equal to x. Some programmers use the @ prefix as a first character when declaring identifiers to avoid a clash with a keyword, but it is not a recommended practice. The following names are valid identifiers in C#:

  1. Hello
  2. hello
  3. H_ello
  4. HelloWorld
  5. X
  6. x
Note: In C#, class names can be different from file names.

You should name the variables using the standard DataType prefixes. Also, the first letter after the prefix should be capitalized. Table 1.6.1 shows a list of prefixes for the various .NET DataTypes. You will learn more about DataTypes in Part 3 of C#—Learning with the FAQs.

Table 1.6.1 List of C# Data Types

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

Interfaces are usually named with an "I" as the first letter. All Windows Forms controls should be named with the special prefixes, as shown in Table 1.6.2. This is to avoid confusion and also to distinguish among other controls in a complex project. As explained above, the first letter after the prefix should be capitalized. Once you master the naming conventions and prefixes, it will be very easy for you to write and debug the code at a later stage.

Table 1.6.2 List of prefixes for Windows Forms controls

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 FAQ to cover the prefixes of all the .NET controls. You will find a detailed list of them in the MSDN Library. (Perform a search using the phrase "naming guidelines")

Keywords

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 a runtime error, as shown in Figure 1.6.1.

Figure 1.6.1

To resolve the problem, you may have to change the class name to some other meaningful name. There are around 80 keywords in C#. They are listed in the table given below.

Table 1.6.3 List of C# Keywords

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
int
interface
internal
is
lock
long
namespace
new
null
object
operator
outo
override
params
private
protected
public
readonly
ref
return
sbyte
sealed
this
throw
true
try
typeof
uint
ulong
unchecked
unsafe
ushort
using
short
sizeof
stackallac
static
string
struct
switch
    virtual
void
 

For more detailed information about this topic, perform a search on the MSDN Library with the phrase "C# keywords."



About the Author

Anand Narayanaswamy

Anand Narayanaswamy (Microsoft MVP) is a freelance writer for Developer.com and Codeguru.com. He works as an independent consultant and runs NetAns Technologies (http://www.netans.com)which provides affordable web hosting services. He is the author of Community Server Quickly (http://www.packtpub.com/community-server/book). Anand also runs LearnXpress.com (http://www.learnXpress.com) and Dotnetalbum.com (http://www.dotnetalbum.com) and regularly contributes product and book reviews for various websites. He can be reached at ananddotnet@yahoo.co.in

Comments

  • Right Here Is A Technique That Is In fact Aiding adidas-industry experts To Expand

    Posted by BobHotgloff on 05/02/2013 09:08am

    Brief study shows you all details for nike and consequently things one ought to do immediately. [url=http://www.mizunogoruhujp.com/]ミズノゴルフ[/url] The Secret For mizuno [url=http://www.mizunogoruhujp.com/ミズノ-ゴルフクラブ-c-1.html]ミズノ アイアン[/url] Compact report helps you with all the details of the nike together with something you must do right now. [url=http://www.mizunogoruhujp.com/ゴルフグローブ-c-33.html]ミズノ グローブ[/url] Everything everybody else is doing in regards to mizuno and the thing that youhas to do completely different. [url=http://www.mizunogoruhujp.com/ゴルフバッグ-c-7.html]ミズノゴルフ[/url] Impartial content material unwraps A number of great new things for nike that none is covering. [url=http://www.mizunogoruhu.com/]ミズノ アイアン[/url] A meaningful double strain on nike [url=http://www.mizunogoruhu.com/ミズノmizuno-クラブ-c-4.html]ミズノ mp[/url] Solutions and construction in Vegas - nike has left without any regards [url=http://www.mizunogoruhu.com/ミズノmizuno-アイアン-c-3.html]ミズノ アイアン[/url] Profitable tips on nike that you can use beginning now. [url=http://www.mizunogoruhu.com/ミズノmizuno-バッグ-c-5.html]ミズノゴルフ[/url] Brief write-up unwraps the incontestable details of mizuno and the way that it can have an effect on your business.

    Reply
  • C# Valid Identifiers

    Posted by Gym.Prathap on 09/20/2012 05:49am

    C# follows The Unicode Standard, Version 3.0 rules except the Underscore You can use the keyword as identifier provided you have prefix with @ character. But it is not a good programming practice. Wisen Technologies http://wisentechnologies.com/it-courses/.net-training.aspx

    Reply
  • C# tutorials for beginners

    Posted by David Allan on 08/11/2012 05:00pm

    The intro to keywords and identifiers is pretty good, however its a bit dated now although still relevant and could use some revitalization. You should visit c-sharp station and learningcsharp.co.uk for beginner tutorials, i found them quite helpful when i was learning.

    Reply
  • feedbacl

    Posted by pawan on 08/04/2012 12:18am

    thanks for the such good article,,,

    Reply
  • Bad practice

    Posted by hzapata on 07/15/2006 12:12pm

    Is a really bad practice use that prefixes, in OOP you should never need it. If you need prefixes to understand your code, then that code need to be refactorized

    Reply
  • Thanks

    Posted by koderum on 05/26/2005 06:40pm

    Thank you for the article. That is an explanatory one.

    Reply
  • from ilango

    Posted by ilango on 03/22/2005 10:45am

    it is very help full fro beginners.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds