Identifiers


In C#, an identifier is a name assigned to a method, a variable, or any other user-defined item. Identifiers can be from one to several characters long. Variable names may start with any letter of the alphabet or an underscore. Next may be a letter, a digit, or an underscore. The underscore can be used to enhance the readability of a variable name, as in line_count. However, identifiers containing two consecutive underscores, such as max_ _value, are reserved for use by the compiler. Uppercase and lowercase are different; that is, to C#, myvar and MyVar are separate names. Here are some examples of acceptable identifiers:

Test

x

y2

MaxLoad

up

_top

my_var

sample23

Remember, you can’t start an identifier with a digit. Thus, 12x is invalid, for example. Good programming practice dictates that you use identifier names that reflect the meaning or usage of the items being named.

Although you cannot use any of the C# keywords as identifier names, C# does allow you to precede a keyword with an @, allowing it to be a legal identifier. For example, @for is a valid identifier. In this case, the identifier is actually for, and the @ is ignored. Here is a program that illustrates the use of an @ identifier:

 // Demonstrate an @ identifier. using System; class IdTest {   public static void Main() {     int @if; // use if as an identifier     for(@if = 0; @if < 10; @if++)       Console.WriteLine("@if is " + @if);   } }

The output shown here proves the @if is properly interpreted as an identifier:

 @if is 0 @if is 1 @if is 2 @if is 3 @if is 4 @if is 5 @if is 6 @if is 7 @if is 8 @if is 9

Frankly, using @-qualified keywords for identifiers is not recommended, except for special purposes. Also, the @ can precede any identifier, but this is considered bad practice.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net