C# Identifiers
Identifiers are names used to denote variables, constants, types, methods, objects, and so on. An identifier begins with a letter or an underscore and ends with the character just before the next white space.
C# identifiers are case sensitive. For example, the three variables deeFlat, DeeFlat, and DEEFLAT are all different, as running this click procedure will show:
private void btnClickMe_Click(object sender, System.EventArgs e) {
string deeFlat = "deeFlat";
string DeeFlat = "DeeFlat";
string DEEFLAT = "DEEFLAT";
MessageBox.Show(deeFlat + " " + DeeFlat + " "
 + DEEFLAT, "Hello, Variables!",
 MessageBoxButtons.OK);
}
Microsoft suggests using “camel notation” for identifiers used as names of variables. Camel notation means an initial lowercase letter followed by uppercase letters (“internal caps”) for initial letters of subsequent words in the identifier. For example: deeFlat.
Pascal notation—an initial uppercase letter, with internal caps as needed—is supposed to be used for identifiers that represent method names and most other non-variable objects. In addition, identifiers used for methods should use a verb-noun combination to describe the purpose of the method, for example GetColorValues.
For more on Microsoft’s suggested identifier naming conventions, look up “Naming Guidelines” using the Search feature in online help.
It is not legal to use an identifier that is also a C# keyword. If, for some really perverse reason, you must use a keyword as an identifier, you can preface the keyword with the @ symbol. For example, if cannot be used as an identifier (because it is a keyword), but @if can; this code snippet shows it employed as a string variable:
string @if = "@if";
MessageBox.Show(@if, "Hello, Variables!", MessageBoxButtons.OK);
For that matter, if you were really bound and determined to create an identifier like a keyword, you could simple vary the case. So If is a perfectly legal identifier—albeit an idiotic choice because it is similar to a keyword and so potentially confusing—because the keyword if is all lowercase.
Visual Basic programmers, who are used to case insensitivity, may find that C#’s case sensitivity regarding identifiers leads to the introduction of bugs. Fortunately, these kinds of bugs are found pretty easily once you are on the watch for them, and paying attention to the case of identifiers will become second nature shortly.
|
|
Search Engine Optimization
 
Syndication Viewer
Our Web host:
IX WebHosting
|