Sams Teach Yourself C# in 24 Hours

Sams Teach Yourself C# in 24 Hours

By James Foxall and Wendy Haro-Chun

Naming Conventions

To make code more self-documenting (always an important goal) and to reduce the chance of programming errors, you need an easy way to determine the exact data type of a variable or the exact type of a referenced control in C# code.

Using Prefixes to Denote Data Type

Table 12.3 lists the prefixes of the common data types.

Table 12.3. Prefixes for Common Data Types

Data Type Prefix Value
Boolean bln blnLoggedIn
Byte byt bytAge
Char chr chrQuantity
Decimal dec decSalary
Double dbl dblCalculatedResult
Integer int intLoopCounter
Long lng lngCustomerID
Object obj objWord
Short sho shoTotalParts
String str strFirstName

Denoting Scope Using Variable Prefixes

Prefixes are useful not only to denote data types, they also can be used to denote scope (see Table 12.4). In particularly large applications, a scope designator is almost a necessity. Again, C# doesn't care whether you use prefixes, but consistently using prefixes benefits you as well as others who have to review your code.

Table 12.4. Prefixes for Variable Scope

Prefix Description Example
g Global g_strSavePath
m Private to class m_blnDataChanged
(no prefix) Nonstatic variable, local to method  

Other Prefixes

Prefixes aren't just for variables. All standard objects (including forms and controls) can use a three-character prefix. There are simply too many controls and objects to list all the prefixes here, although you will find that I use control prefixes throughout this book.

Share ThisShare This

Informit Network