C# Programming Guidelines C# Help

The final section of this chapter supplies the guidelines you need to bear in mind when writing C# programs.

Rules for Identifiers

 This section examines the rules governing what names you can use for variables, classes, methods, and so on. Note that the rules presented in this section are not merely guidelines: they are’ enforced by the C# compiler.

Identifiers are the names you give to variables, to user-defined types such as classes and structs, and to members of these types. Identifiers are case sensitive, so, for example, variables named interestRate and Interest rate would be recognized as different variables. Following are a few rules determining what identifiers you can use in C#:

  1. They must begin with a letter or underscore, although they can contain numeric characters.
  2. You can’t use C# keywords as identifiers.

The following table lists the C# reserved keywords.

If you do need to use one of these words as an identifier (for  example, if you are accessing a class written in a different  language), you 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:

  1.  Name ..
  2. UberfluB
  3. Identifier
  4. uOO5fIdentifier

The last two items in this list are identical and interchangeable  because 005f is the Unicode code for the underscore character); so obviously these identifiers couldn’t both be declared in the same  scope. Note that although syntactically you are allowed to use the underscore character in identifiers, this isn’t recommended in most situations. That’s because it doesn’t follow the guidelines for naming variables  that Microsoft has written to ensure that developers use the same conventions, making it easier to read each other’s code.

Posted on October 27, 2015 in C# Basics

Share the Story

Back to Top