15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

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

158 ❘ ChaPTer 7 OperAtOrs And cAsts<br />

<strong>C#</strong> also supports conversions between different reference types <strong>and</strong> allows you to<br />

defi ne how data types that you create behave when converted to <strong>and</strong> from other types.<br />

Both of these topics are discussed later in this chapter.<br />

Generics, on the other h<strong>and</strong>, allow you to avoid some of the most common situations<br />

in which you would need to perform type conversions. See Chapter 5, “Generics” <strong>and</strong><br />

Chapter 10, “Collections,” for details.<br />

Type Conversions<br />

Often, you need to convert data from one type to another. Consider the following code:<br />

byte value1 = 10;<br />

byte value2 = 23;<br />

byte total;<br />

total = value1 + value2;<br />

Console.WriteLine(total);<br />

When you attempt to compile these lines, you get the following error message:<br />

Cannot implicitly convert type ‘ int ’ to ‘ byte'<br />

The problem here is that when you add 2 bytes together, the result will be returned as an int , not as<br />

another byte . This is because a byte can contain only 8 bits of data, so adding 2 bytes together could very<br />

easily result in a value that cannot be stored in a single byte . If you do want to store this result in a byte<br />

variable, you are going to have to convert it back to a byte . The following sections discuss two conversion<br />

mechanisms supported by <strong>C#</strong> — implicit <strong>and</strong> explicit.<br />

implicit Conversions<br />

Conversion between types can normally be achieved automatically (implicitly) only if you can guarantee<br />

that the value is not changed in any way. This is why the previous code failed; by attempting a conversion<br />

from an int to a byte , you were potentially losing 3 bytes of data. The compiler is not going to let you do<br />

that unless you explicitly tell it that that ’ s what you want to do. If you store the result in a long instead of a<br />

byte , however, you will have no problems:<br />

byte value1 = 10;<br />

byte value2 = 23;<br />

long total;<br />

total = value1 + value2;<br />

Console.WriteLine(total);<br />

// this will compile fine<br />

Your program has compiled with no errors at this point because a long holds more bytes of data than<br />

a byte , so there is no risk of data being lost. In these circumstances, the compiler is happy to make the<br />

conversion for you, without your needing to ask for it explicitly.<br />

The following table shows the implicit type conversions supported in <strong>C#</strong>:<br />

from<br />

sbyte<br />

byte<br />

short<br />

ushort<br />

int<br />

uint<br />

To<br />

short, int, long, float, double, decimal, BigInteger<br />

short, ushort, int, uint long, ulong, float, double, decimal,<br />

BigInteger<br />

int, long, float, double, decimal, BigInteger<br />

int, uint, long, ulong, float, double, decimal, BigInteger<br />

long, float double, decimal, BigInteger<br />

long, ulong, float, double, decimal, BigInteger<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!