22.02.2016 Views

C Programming Yellow Book

6019BjHWX

6019BjHWX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Simple Data Processing<br />

Manipulating Data<br />

to perform will cause data to be lost from the program. It considers every operation in<br />

terms of "widening and narrowing" values.<br />

Widening and Narrowing<br />

The general principle which C# uses is that if you are "narrowing" a value it will<br />

always ask you to explicitly tell it that this is what you want to do. If you are widening<br />

there is no problem.<br />

To understand what we mean by these terms we could consider suitcases. If I am<br />

packing for a trip I will take a case. If I decide to switch to a smaller case I will have to<br />

take everything out of the large case and put it into the smaller one. But it might not<br />

have room, so I have to leave behind one of my shirts. This is "narrowing".<br />

However, if I change to a bigger case there is no problem. The bigger case will take<br />

everything that was in the smaller case and have room for more.<br />

In C# terms the "size" of a type is the range of values (the biggest and smallest) and the<br />

precision (the number of decimal places). This means that if I write:<br />

int i = 1 ;<br />

float x = i;<br />

This works fine because the floating point type can hold all the values supported by the<br />

integer type. However:<br />

float x = 1;<br />

int i = x ;<br />

- would cause the compiler to complain (even though at the moment the variable x only<br />

holds an integer value). The compiler is concerned that you may be discarding<br />

information by such an assignment and treats it as an error.<br />

Note that this applies within floating point values as well, for example:<br />

double d = 1.5;<br />

float f = d ;<br />

- would cause an error as well, since the compiler knows that a double is wider than a<br />

float.<br />

Casting<br />

We can force C# to regard a value as being of a certain type by the use of casting. A<br />

cast takes the form of an additional instruction to the compiler to force it to regard a<br />

value in a particular way. You cast a value by putting the type you want to see there in<br />

brackets before it. For example:<br />

double d = 1.5;<br />

float f = (float) d ;<br />

In the above code the message to the compiler is "I don't care that this assignment<br />

could cause the loss of information. I, as the writer of the program, will take the<br />

responsibility of making sure that the program works correctly". You can regard<br />

casting as the compiler's way of washing its hands of the problem. If a program fails<br />

because data is lost it is not because the compiler did something silly.<br />

As we saw above, each type of variable has a particular range of possible values, and<br />

the range of floating point values is much greater than that for integers. This means that<br />

if you do things like this:<br />

int i ;<br />

i = (int) 123456781234567890.999 ;<br />

I do not think of this as a<br />

failing in C#. It gives you<br />

great flexibility, at the cost of<br />

assuming you know what you<br />

are doing....<br />

- the cast is doomed to fail. The value which gets placed in i will be invalid. Nothing<br />

in C# checks for mistakes like this. It is up to you when you write your program to<br />

make sure that you never exceed the range of the data types you are using - the<br />

program will not notice but the user certainly will!<br />

C# <strong>Programming</strong> © Rob Miles 2015 30

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!