Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать
NEW TERM

72

Day 3

LISTING 3.9 continued

15:Console.WriteLine(“\nmy_variable is {0}”, my_variable );

16:Console.WriteLine(“\nPI is {0}”, PI );

17:}

18:}

OUTPUT

my_variable is 4

 

 

PI is 3.1459

ANALYSIS Lines 12 and 13 declare an int and a double. Lines 15 and 16 print these values. This listing operates like those you’ve seen earlier, except it uses the .NET data

types.

In your C# programs, you should use the simple data types rather than the .NET types. All the functionality that the .NET types have is available to you in the simpler commands that C# provides. You should, however, understand that the simple C# data types translate to .NET equivalents. You’ll find that all other programming languages that work with the Microsoft .NET types also have data types that translate to these .NET types.

Note

The Common Type System (CTS) is a set of rules that data types within the CLR must adhere to. The simple data types within C# adhere to these rules, as do the .NET data types. If a language follows the CTS in creating its data types, the data created and stored should be compatible with other programming languages that also follow the CTS.

Literals Versus Variables

In the examples you’ve looked at so far, you have seen a lot of numbers and values used that were not variables. Often, you will want to type a number or value

into your source code. A literal value stands on its own within the source code. For example, in the following lines of code, the number 10 and the value “Bob is a fish” are literal values. As you can see, literal values can be put into variables.

int x = 10;

myStringValue = “Bob is a fish”;

Numeric Literals

In many of the examples, you have used numeric literals. By default, a numeric literal is either an int or a double. It is an int if it is a whole number, and it is a double if it is a floating-point number. For example, consider the following:

Storing Information with Variables

73

nbr = 100;

In this example, 100 is a numeric literal. By default, it is considered to be of type int, regardless of what data type the nbr variable is. Now consider the following:

nbr = 99.9;

In this example, 99.9 is also a numeric literal; however, it is of type double by default. Again, this is regardless of the data type that nbr is. This is true even though 99.9 could be stored in a type float. In the following line of code, is 100. an int or a double?

x = 100.;

This is a tough one. If you guessed int, you are wrong. Because there is a decimal

 

included with the 100, it is a double.

 

Integer Literal Defaults

3

When you use an integer value, it is put into an int, uint, long, or ulong depending on its size. If it will fit in an int or uint, it will be. If not, it will be put into a long or ulong. If you want to specify the data type of the literal, you can use a suffix on the literal. For example, to use the number 10 as a literal long value(signed or unsigned), you write it like the following:

10L;

You can make an unsigned value by using a u or a U. If you want an unsigned literal long value, you can combine the two suffixes: ul.

Note

The Microsoft C# compiler gives you a warning if you use a lowercase l to declare a long value literal. The compiler provides this warning to help you be aware that it is easy to mistake a lowercase l with the number 1.

Floating-Point Literal Defaults

As stated earlier, by default, a decimal value literal is a double. To declare a literal that is of type float, you include an f or F after the number. For example, to assign the number 4.4 to a float variable, my_float, you use the following:

my_float = 4.4f;

To declare a literal of type decimal, you use a suffix of m or M. For example, the following line declares my_decimal to be equal to the decimal number 1.32.

my_decimal = 1.32m;

74

Day 3

Boolean Literals (true and false)

Boolean literals have already been covered. The values true and false are literal. They also happen to be keywords.

String Literals

When you put characters together, they make words, phrases, and sentences. In programming parlance, a group of characters is called a string. A string can be identified because it is contained between a set of double quotes. You have actually been using strings in the examples so far. For example, the Console.WriteLine routine uses a string. A string literal is any set of characters between double quotes. The following are examples of strings:

“Hello, World!”

“My Name is Bradley”

“1234567890”

Because these numbers are between quotation marks, the last example is treated as a string literal rather than as a numeric literal.

Note

You can use any of the special characters from Table 3.3 inside a string.

Constants

In addition to using literals, there are times when you want to put a value in a variable and freeze it. For example, if you declare a variable called PI and you set it to 3.1459, you want it to stay 3.1459. There is no reason to change it. Additionally, you want to prevent people from changing it.

To declare a variable to hold a constant value, you use the const keyword. For example, to declare PI as stated, you use the following:

const float PI = 3.1459;

You can use PI in a program; however, you will never be able to change its value. The const keyword freezes its contents. You can use the const keyword on any variable of any data type.

Storing Information with Variables

75

Tip

To help make it easy to identify constants, you can enter their names in all capital letters. This makes it easy to identify the fact that the variable is a constant.

Reference Types

To this point, you have seen a number of different data types. C# offers two primary ways of storing information: by value (byval) and by reference (byref). The basic data types that you have learned about store information by value.

When a variable stores information by value, the variable contains the actual information.

 

For example, when you store 123 in an integer variable called x, the value of x is 123.

3

The variable x actually contains the value 123.

Storing information by reference is a little more complicated. If a variable stores by reference, rather than storing the information in itself, it stores the location of the information. In other words, it stores a reference to the information. For example, if x is a “by reference” variable, it contains information on where the value 123 is located. It does not store the value 123. Figure 3.1 illustrates the difference.

FIGURE 3.1

 

 

By reference versus by

 

 

value.

1 2 3

Memory

 

X_byref

X_byval

 

The data types used by C# that store by reference are

Classes

Strings

Interfaces

Arrays

Delegates

Each of these data types is covered in detail throughout the rest of this book.