Type Safety C# Help

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, 0# 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.

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

Generics, aperture included in C#, allows you to avoid some of the most common situations in which you would need to perform type conversions.

Type Conversions

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

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

When you attempt to compile these lines, you get the following 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 contain only 8 bits of data, so adding 2 bytes together could very easily result in a value that cannot be stored in a single byte. If you do want to store this result in a byte variable, you are going to have to convert it back to a byte. The following sections discuss two conversion mechanisms supported by 0# – 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 is not 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 will have no problems:

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 seeding to ask for it explicitly,

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

As you would expect, you can perform implicit conversions only from a smaller integer type to a larger one, not from larger to smaller. Youcan 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. Youcan 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:

  1.  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?
  2.  Non-nullable types implicitly convert to nullable types accord g to the conversion rules described in the preceding table; that is, int implicitly converts to long?, float?, double?, and decimal?
  3.  Nullable types do not implicitly convert to non-null able types; you must perform 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:

  1. int . to short – Data loss is possible . o irtt to uint – Data loss is possible.  ui,nt to int – Data loss is possible.
  2.  float to int – Youwill lose everything after the decimal point.
  3.  Any numeric type to chaz – Data loss is possible.
  4.  decimal to any numeric type – The decimal type is internally structured differently from both integers and floating-point numbers.
  5. 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; II A valid cast. The maximumint is 2147483647

You indicate the type to which you are 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, note that these do not exist in C# and that 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 gre~ter than the maximum value of an int:

long val = 3000000000;
int i = (int)val; II An invalid cast. The maximumint 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 is not:’

long val = 300000000~;
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.

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. Youcan try 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 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 It~etails
{
public string Description;
public int ApproxPrice;
II …
doubler] Prices { 25.30, 26.20, 27.40, 30.00 );
ItemDetails id;
id.Description = ‘Whatever’;
id.ApproxPrice = (int) (Prices[O] + 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. 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 art explicit castbetween two equivalent non-nullable types is possible, so is the explicit cast between nullable types. However, when 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 cannot directly cast Booleans to any other type or vice versa.

If you need to convert between numeric and string, you can use methods provided in the .NET class library. The Obj ect 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);          II 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)

Boxing and Unboxing

 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 litera 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 tlUsis 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. We include this 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 refererce-type box for the object on the heap.

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

int mylntNumber = 20;
object myObject = mylntNumber

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. Weuse the term CIlSt here, because this has to be done explicitly. The syntax is similar to explicit type conversions already described:

You can only unbox a variable that has previously been boxed. If you execute the last line when myObject 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 (64bits) into an int as shown here will result in an InvalidCastException:

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

Posted on October 28, 2015 in Operators and Casts

Share the Story

Back to Top