Working with Primitive Data Types
C# has a number of built-in types called primitive data types. The following table lists the most commonly used primitive data types in C#, and the ranges of values that you can store in them.
Data type | Description | Size (bits) | *Range | Sample usage |
int | Whole numbers | 32 | <–>231 through 231<–>1 |
int count; count = 42; |
long | Whole numbers (bigger range) | 64 | <–>263 through 263<–>1 |
long wait; wait = 42L; |
float | Floating-point numbers | 32 | ±3.4 × 1038 |
float away; away = 0.42F; |
double | Double precision (more accurate) floating-point numbers | 64 | ±1.7 × 10308 |
double trouble; trouble = 0.42; |
decimal | Monetary values | 128 | 28 significant figures |
decimal coin; coin = 0.42M; |
string | Sequence of characters | 16 bits per character | Not applicable |
string vest; vest = "42"; |
char | Single character | 16 | 0 through 216 <–>1 |
char grill; grill = '4'; |
bool | Boolean | 8 | true or false |
bool teeth; teeth = false; |
*The value of 216 is 32,768; the value of 231 is 2,147,483,648; and the value of 263 is 9,223,372,036,854,775,808. |
Unassigned Local Variables
When you declare a variable, it contains a random value until you assign a value to it. This behavior was a rich source of bugs in C and C++ programs that created a variable and used it as a source of information before giving it a value. C# does not allow you to use an unassigned variable. You must assign a value to a variable before you can use it, otherwise your program will not compile. This requirement is called the Definite Assignment Rule. For example, the following statements will generate a compile-time error because age is unassigned:
int age; Console.WriteLine(age); // compile time error
Displaying Primitive Data Type Values
In the following exercise, you'll use a C# program named PrimitiveDataTypes to demonstrate how several primitive data types work.
Display primitive data type values
Start Visual Studio 2005.
On the File menu, point to Open, and then click Project/Solution.
The Open Project dialog box appears.
Move to the \Microsoft Press\Visual CSharp Step by Step\Chapter 2\PrimitiveDataTypes folder in your My Documents folder. Select the file PrimitiveDataTypes.sln and then click Open.
The solution loads, and the Solution Explorer displays the solution and PrimitiveDataTypes project.
NOTE
Solution file names have the .sln suffix, such as PrimitiveDataTypes.sln. A solution can contain one or more projects. Project files have the .csproj suffix. If you open a project rather than a solution, Visual Studio 2005 will automatically create a new solution file for it. If you build the solution, Visual Studio 2005 automatically saves any updated or new files, and you will be prompted to provide a name and location for the new solution file.
On the Debug menu, click Start Without Debugging.
The following application window appears:
In the Choose A Data type list, click the string type.
The value 42 appears in the Sample value box.
Click the int type in the list.
The value to do appears in the Sample value box, indicating that the statements to display an int value still need to be written.
Click each data type in the list. Confirm that the code for the double and bool types also needs to be completed.
Click Quit to close the window and stop the program.
Control returns to the Visual Studio 2005 programming environment.
Use primitive data types in code
Right-click the Form1.cs file in the Solution Explorer and then click View Code.
The Code and Text Editor window opens displaying the Form1.cs file.
In the Code and Text Editor window, find the showFloatValue method listed here:
private void showFloatValue() { float var; var = 0.42F; value.Text = "0.42F"; }
TIP
To locate an item in your project, point to Find And Replace on the Edit menu and click Quick Find. A dialog box opens asking what you want to search for. Type the name of the item you're looking for, and then click Find Next. By default, the search is not case-sensitive. If you want to perform a case-sensitive search, click the + button next to the Find Options label to display additional options, and check the Match Case check box. If you have time, you can experiment with the other options as well.
You can also press Ctrl+F (press the Control key, and then press F) to display the Quick Find dialog box rather then using the Edit menu. Similarly, you can press Ctrl+H to display the Quick Find and Replace dialog box.
The showFloatValue method runs when you click the float type in the list box. This method contains three statements:
The first statement declares a variable named var of type float.
The second statement assigns var the value 0.42F. (The F is a type suffix specifying that 0.42 should be treated as a float value. If you forget the F, the value 0.42 will be treated as a double, and your program will not compile because you cannot assign a value of one type to a variable of a different type in this way.)
The third statement displays the value of this variable in the value TextBox on the form. This statement requires a little bit of your attention. The way in which you display an item in a TextBox is to set its Text property. You did this at design time in Chapter 1 using the Properties window. This statement shows you how to perform the same task programmatically, using the expression value.Text. The data that you put in the Text property must be a string (a sequence of characters), and not a number. If you try and assign a number to the Text property your program will not compile. For this reason, the statement simply displays the text “0.42F” in the TextBox (anything in double-quotes is text, otherwise known as a string). In a real-world application, you would add statements that convert the value of the variable var into a string and then put this into the Text property, but you need to know a little bit more about C# and the .NET Framework before we can do that (we will cover data type conversions in Chapter 11, “Understanding Parameter Arrays,” and Chapter 19, “Operator Overloading”).
In the Code and Text Editor window, locate the showIntValue method listed here:
private void showIntValue() { value.Text = "to do"; }
The showIntValue method is called when you click the int type in the list box.
TIP
Another way to find a method in the Code and Text Editor window is to click the Members list that appears above the window, to the right. This window displays a list of all the methods (and other items). You can click the name of a member, and you will be taken directly to it in the Code and Text Editor window.
Type the following two statements at the start of the showIntValue method, after the open curly brace:
int var; var = 42;
The showIntValue method should now look like this:
private void showIntValue() { int var; var = 42; value.Text = "to do"; }
On the Build menu, click Build Solution.
The build will display some warnings, but no errors. You can ignore the warnings for now.
In the original statement, change the string “to do” to “42”.
The method should now look exactly like this:
private void showIntValue() { int var; var = 42; value.Text = "42"; }
On the Debug menu, click Start Without Debugging.
The form appears again.
TIP
If you have edited the source code since the last build, the Start Without Debugging command automatically rebuilds the program before starting the application.
Select the int type in the list box. Confirm that the value 42 is displayed in the Sample value text box.
Click Quit to close the window and stop the program.
In the Code and Text Editor window, find the showDoubleValue method.
Edit the showDoubleValue method exactly as follows:
private void showDoubleValue() { double var; var = 0.42; value.Text = "0.42"; }
In the Code and Text Editor window, locate the showBoolValue method.
Edit the showBoolValue method exactly as follows:
private void showBoolValue() { bool var; var = false; value.Text = "false"; }
On the Debug menu, click Start Without Debugging.
The form appears.
In the list, select the int, double, and bool types. In each case, verify that the correct value is displayed in the Sample value text box.
Click Quit to stop the program.