Type Safety


Chapter 1, “.NET Architecture,” noted that the Intermediate Language (IL) enforces strong type safety upon its code. Strong typing enables many of the services provided by .NET, including security and language interoperability. As you would expect from a language compiled into IL, C# is also strongly typed. Among other things, this means that data types are not always seamlessly interchangeable. This section looks at conversions between primitive types.

Tip 

C# also supports conversions between different reference types and allows you to define how data types that you create behave when converted to and from other types. Both these topics are discussed later in this chapter.

Generics, a new feature included in C# 2.0, allows you to avoid some of the most common situations in which you would need to perform type conversions. See Chapter 9, “Generics,” for details.

Type Conversions

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

  byte value1 = 10; byte value2 = 23; byte total; total = value1 + value2; Console.WriteLine(total); 

When you attempt to compile these lines, you get the error message:

 Cannot implicitly convert type 'int' to 'byte'

The problem here is that when you add 2 bytes together, the result will be returned as an int, not as another byte. This is because a byte can only contain 8 bits of data, so adding 2 bytes together could very easily result in a value that can’t be stored in a single byte. If you do want to store this result in a byte variable, you’re going to have to convert it back to a byte. The following sections discuss two conversion mechanisms supported by C# - implicit and explicit.

Implicit conversions

Conversion between types can normally be achieved automatically (implicitly) only if you can guarantee that the value is not changed in any way. This is why the previous code failed; by attempting a conversion from an int to a byte, you were potentially losing 3 bytes of data. The compiler isn’t going to let you do 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 byte, however, you’ll have no problems:

 byte value1 = 10; byte value2 = 23; long total;               // this will compile fine total = value1 + value2; Console.WriteLine(total);

Your program has compiled with no errors at this point because a long holds more bytes of data than a byte, so there is no risk of data being lost. In these circumstances, the compiler is happy to make the conversion for you, without your needing to ask for it explicitly.

The following table shows the implicit type conversions supported in C#.

Open table as spreadsheet

From

To

sbyte

short, int, long, float, double, decimal

byte

short, ushort, int, uint, long, ulong, float, double, decimal

short

int, long, float, double, decimal

ushort

int, uint, long, ulong, float, double, decimal

int

long, float, double, decimal

uint

long, ulong, float, double, decimal

long, ulong

float, double, decimal

float

Double

char

ushort, int, uint, long, ulong, float, double, decimal

As you would expect, you can only perform implicit conversions from a smaller integer type to a larger one, not from larger to smaller. You can also convert between integers and floating-point values; however, the rules are slightly different here. Though you can convert between types of the same size, such as int/uint to float and long/ulong to double, you can also convert from long/ulong back to float. You might lose 4 bytes of data doing this, but this only means that the value of the float you receive will be less precise than if you had used a double; this is regarded by the compiler as an acceptable possible error because the magnitude of the value is not affected. You can also assign an unsigned variable to a signed variable as long as the limits of value of the unsigned type fit between the limits of the signed variable.

Nullable types introduce additional considerations when implicitly converting value types:

  • Nullable types implicitly convert to other nullable types following the conversion rules described for non-nullable types in the previous table; that is, int? implicitly converts to long?, float?, double?, and decimal?.

  • Non-nullable types implicitly convert to nullable types according to the conversion rules described in the preceding table; that is, int implicitly converts to long?, float?, double?, and decimal?.

  • Nullable types do not implicitly convert to non-nullable types; you must perform an explicit conversion as described in the next section. This is because there is the chance a nullable type will have the value null, which cannot be represented by a non-nullable type.

Explicit Conversions

Many conversions cannot be implicitly made between types, and the compiler will give you an error if any are attempted. These are some of the conversions that cannot be made implicitly:

  • int to short - Data loss is possible.

  • int to uint - Data loss is possible.

  • uint to int - Data loss is possible.

  • float to int - You will lose everything after the decimal point.

  • Any numeric type to char - Data loss is possible.

  • Decimal to any numeric type - Because the decimal type is internally structured differently from both integers and floating-point numbers.

  • int? to int - The nullable type may have the value null.

However, you can explicitly carry out such conversions using casts. When you cast one type to another, you deliberately force the compiler to make the conversion. A cast looks like this:

  long val = 30000; int i = (int)val;   // A valid cast. The maximum int is 2147483647 

You indicate the type to which you’re casting by placing its name in parentheses before the value to be converted. If you are familiar with C, this is the typical syntax for casts. If you are familiar with the C++ special cast keywords such as static_cast, these do not exist in C# and you have to use the older C-type syntax.

Casting can be a dangerous operation to undertake. Even a simple cast from a long to an int can cause problems if the value of the original long is greater than the maximum value of an int:

  long val = 3000000000; int i = (int)val;         // An invalid cast. The maximum int is 2147483647 

In this case, you will not get an error, but you also will not get the result you expect. If you run this code and output the value stored in i, this is what you get:

 -1294967296

It is good practice to assume that an explicit cast will not give the results you expect. As you saw earlier, C# provides a checked operator that you can use to test whether an operation causes an arithmetic overflow. You can use the checked operator to check that a cast is safe and to force the runtime to throw an overflow exception if it isn’t:

 long val = 3000000000; int i = checked((int)val); 

Bearing in mind that all explicit casts are potentially unsafe, you should take care to include code in your application to deal with possible failures of the casts. Chapter 13, “Errors and Exceptions,” introduces structured exception handling using the try and catch statements.

Using casts, you can convert most primitive data types from one type to another; for example, in this code the value 0.5 is added to price, and the total is cast to an int:

  double price = 25.30; int approximatePrice = (int)(price + 0.5); 

This will give the price rounded to the nearest dollar. However, in this conversion, data is lost - namely everything after the decimal point. Therefore, such a conversion should never be used if you want to go on to do more calculations using this modified price value. However, it is useful if you want to output the approximate value of a completed or partially completed calculation - if you do not want to bother the user with lots of figures after the decimal point.

This example shows what happens if you convert an unsigned integer into a char:

  ushort c = 43; char symbol = (char)c; Console.WriteLine(symbol); 

The output is the character that has an ASCII number of 43, the + sign. You can try out any kind of conversion you want between the numeric types (including char), and it will work, such as converting a decimal into a char, or vice versa.

Converting between value types is not just restricted to isolated variables, as you have seen. You can convert an array element of type double to a struct member variable of type int:

  struct ItemDetails {    public string Description;    public int ApproxPrice; } //... double[] Prices = { 25.30, 26.20, 27.40, 30.00 }; ItemDetails id; id.Description = "Whatever"; id.ApproxPrice = (int)(Prices[0] + 0.5); 

To convert a nullable type to a non-nullable type or another nullable type where data loss may occur, you must use an explicit cast. Importantly, this is true even when converting between elements with the same basic underlying type, for example, int? to int or float? to float. This is because the nullable type may have the value null, which cannot be represented by the non-nullable type. As long as an explicit cast between two equivalent non-nullable types is possible, so is the explicit cast between nullable types. However, if casting from a nullable to non-nullable type and the variable has the value null, an InvalidOperationException is thrown. For example:

  int? a = null; int  b = (int)a;     // Will throw exception 

Using explicit casts and a bit of care and attention, you can convert any instance of a simple value type to almost any other. However, there are limitations on what you can do with explicit type conversions - as far as value types are concerned, you can only convert to and from the numeric and char types and enum types. You can’t directly cast Booleans to any other type or vice versa.

If you need to convert between numeric and string, methods are provided in the .NET class library. The Object class implements a ToString() method, which has been overridden in all the .NET predefined types and which returns a string representation of the object:

  int i = 10; string s = i.ToString(); 

Similarly, if you need to parse a string to retrieve a numeric or Boolean value, you can use the Parse() method supported by all the predefined value types:

  string s = "100"; int i = int.Parse(s); Console.WriteLine(i + 50);   // Add 50 to prove it is really an int 

Note that Parse() will register an error by throwing an exception if it is unable to convert the string (for example, if you try to convert the string Hello to an integer). Again, exceptions are covered in Chapter 13.

Boxing and Unboxing

In Chapter 2, “C# Basics,” you learned that all types, both the simple predefined types such as int and char, and the complex types such as classes and structs, derive from the object type. This means that you can treat even literal values as though they were objects:

  string s = 10.ToString(); 

However, you also saw that C# data types are divided into value types, which are allocated on the stack, and reference types, which are allocated on the heap. How does this square with the ability to call methods on an int, if the int is nothing more than a 4-byte value on the stack?

The way C# achieves this is through a bit of magic called boxing. Boxing and its counterpart, unboxing, allow you to convert value types to reference types and then back to value types. This is included in the section on casting because this is essentially what you are doing - you are casting your value to the object type. Boxing is the term used to describe the transformation of a value type to a reference type. Basically, the runtime creates a temporary reference-type box for the object on the heap.

This conversion can occur implicitly, as in the preceding example, but you can also perform it manually:

  int myIntNumber = 20; object myObject = myIntNumber; 

Unboxing is the term used to describe the reverse process, where the value of a previously boxed value type is cast back to a value type. We use the term cast here, because this has to be done explicitly. The syntax is similar to explicit type conversions already described:

  int myIntNumber = 20; object myObject = myIntNumber;     // Box the int int mySecondNumber = (int)myObject;   // Unbox it back into an int 

You can only unbox a variable that has previously been boxed. If you executed the last line when o is not a boxed int, you will get an exception thrown at runtime.

One word of warning: when unboxing, you have to be careful that the receiving value variable has enough room to store all the bytes in the value being unboxed. C#’s ints, for example, are only 32 bits long, so unboxing a long value (64 bits) into an int as shown here will result in an InvalidCastException:

  long myLongNumber = 333333423; object myObject = (object)myLongNumber; int myIntNumber = (int)myObject; 




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net