Building the Right Environment to Support AI, Machine Learning and Deep Learning
This is Day 3, "Storing Information with Variables," from Sams Teach Yourself C# in 21 Days. More information about the book appears at the end of this chapter.
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.
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.
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:
|_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.
Before you can use a variable in a C# program, you must declare it. A variable declaration tells the compiler the name of the variable and the type of information the variable will be used to store. If your program attempts to use a variable that hasn't been declared, the compiler will generate an error message.
Declaring a variable also enables the computer to set aside memory for it. By identifying the specific type of information that will be stored in a variable, you gain the best performance and avoid wasting memory.
Declaring a Variable
A variable declaration has the following form:
typename specifies the variable type. In the following sections you will learn about the types of variables that are available in C#. varname is the name of the variable. To declare a variable that can hold a standard numeric integer, you use the following line of code:
The name of the variable declared is my_number. The data type of the variable is int. As you will learn in the following section, the type int is used to declare integer variables, which is perfect for this example!
You can also declare multiple variables of the same type on one line by separating the variable names with commas. This enables you to be more concise in your listings. Consider the following line:
int count, number, start;
This line declares three variables: count, number, and start. Each of these variables is type int, which is for integers.
Note: Although declaring multiple variables on the same line can be more concise, I don't recommend that you always do this. There are times when it is easier to read and follow your code by using multiple declarations. There will be no noticeable performance loss by doing separate declarations.