Home > Articles > Programming > C#

Storing Information with Variables in C#

  • Print
  • + Share This
When you start writing programs, you'll quickly find that you need to keep track of different types of information. This might be the tracking of your clients' names, the amounts of money in your bank accounts, or the ages of your favorite movie stars. To keep track of this information, your computer programs need a way to store the values. In this sample chapter, you'll learn what a variable is, as well as how to: create variable names in C#, use different types of numeric variables, evaluate the differences and similarities between character and numeric values, and declare and initialize variables.
This sample chapter is excerpted from Sams Teach Yourself C# in 21 Days, by Bradley Jones.
This chapter is from the book

This chapter is from the book

Variables

A variable is a named data storage location in your computer's memory. By using a variable's name in your program, you are, in effect, referring to the information stored there. For example, you could create a variable called my_variable that holds a number. You would be able to store different numbers in the my_variable variable.

You could also create variables to store information other than a simple number. You could create a variable called BankAccount to store a bank account number, a variable called email to store an email address, or a variable called address to store a person's mailing address. Regardless of what type of information will be stored, a variable is used to obtain its value.

Variable Names

To use variables in your C# programs, you must know how to create variable names. In C#, variable names must adhere to the following rules:

  • The name can contain letters, digits, and the underscore character (_).

  • The first character of the name must be a letter. The underscore is also a legal first character, but its use is not recommended at the beginning of a name. An underscore is often used with special commands, and it's sometimes hard to read.

  • Case matters (that is, upper- and lowercase letters). C# is case-sensitive; thus, the names count and Count refer to two different variables.

  • C# keywords can't be used as variable names. Recall that a keyword is a word that is part of the C# language. (A complete list of the C# keywords can be found in Appendix B, "C# Keywords.")

The following list contains some examples of valid and invalid C# variable names:

Variable Name

Legality

Percent

Legal

y2x5__w7h3

Legal

yearly_cost

Legal

_2010_tax

Legal, but not advised

checking#account

Illegal; contains the illegal character #

double

Illegal; is a C keyword

9byte

Illegal; first character is a digit


Because C# is case-sensitive, the names percent, PERCENT, and Percent are considered three different variables. C# programmers commonly use only lowercase letters in variable names, although this isn't required. Often, programmers use mixed case as well. Using all-uppercase letters is usually reserved for the names of constants (which are covered later today).

Variables can have any name that fits the rules listed previously. For example, a program that calculates the area of a circle could store the value of the radius in a variable named radius. The variable name helps make its usage clear. You could also have created a variable named x or even billy_gates; it doesn't matter. Such a variable name, however, wouldn't be nearly as clear to someone else looking at the source code. Although it might take a little more time to type descriptive variable names, the improvements in program clarity make it worthwhile.

Many naming conventions are used for variable names created from multiple words. Consider the variable name circle_radius. Using an underscore to separate words in a variable name makes it easy to interpret. Another style is called Pascal notation. Instead of using spaces, the first letter of each word is capitalized. Instead of circle_radius, the variable would be named CircleRadius. Yet another notation that is growing in popularity is Camel notation. Camel notation is like Pascal notation, except the first letter of the variable name is also lower case. A special form of Camel notation is called Hungarian notation. With Hungarian notation, you also include information in the name of the variable—such as whether it is numeric, has a decimal value, or is text—that helps to identify the type of information being stored. The underscore is used in this book because it's easier for most people to read. You should decide which style you want to adopt.

DO use variable names that are descriptive.

DO adopt and stick with a style for naming your variables.

DON'T name your variables with all capital letters unnecessarily.

NOTE

C# supports a Unicode character set, which means that letters from any language can be stored and used. You can also use any Unicode character to name your variables.

  • + Share This
  • 🔖 Save To Your Account