Chapter 2 - C# Basics | |
bySimon Robinsonet al. | |
Wrox Press 2002 | |
In this final section of this basics of C# chapter, we examine the rules governing what names we can use for variables , classes, methods , and so on.
Identifiers are the names we give to variables, to user -defined types such as classes and structs, and to members of these types. Identifiers are case sensitive, so identifier and Identifier would be different variables. There are a couple of rules determining what identifiers we can use in C#:
They must begin with a letter or underscore , although they can contain numeric characters
We can't use C# keywords as identifiers
C# has the following reserved keywords:
abstract | do | implicit | params | switch |
as | double | in | private | this |
base | else | int | protected | throw |
bool | enum | interface | public | true |
break | event | internal | readonly | try |
byte | explicit | is | ref | typeof |
case | extern | lock | return | uint |
catch | false | long | sbyte | ulong |
char | finally | namespace | sealed | unchecked |
checked | fixed | new | short | unsafe |
class | float | null | sizeof | ushort |
const | for | object | stackalloc | using |
continue | foreach | operator | static | virtual |
decimal | goto | out | string | volatile |
default | if | override | struct | void |
delegate | while |
If we do need to use one of these words as an identifier (for example, if we are accessing a class written in a different language), we can prefix the identifier with the @ symbol to indicate to the compiler that what follows is to be treated as an identifier, not as a C# keyword (so abstract is not a valid identifier, but @abstract is).
Finally, identifiers can also contain Unicode characters, specified using the syntax \uXXXX , where XXXX is the four-digit hex code for the Unicode character. The following are some examples of valid identifiers:
Name