CSharp Bits is a tutorial-based digest explaining the C# programming language. It evolves, covering prerequisite topics and is currently broad in coverage rather than getting deep into a specific topic. That will come after a basis has been established.  It is an internal Microsoft distribution that I submit emails to on a daily basis (approximately).  The issues mainly cover one topic each, so readers can fit it in their daily routine.  I have been asked to blog the digests externally to reach out to the user community.

 

All code examples are meant to demonstrate the topic only. It is neither shippable nor secure code by any stretch of the imagination.  An archive of previous topics is not currently available externally.

 

Data Conversions

In the upcoming issues, I will explain how to perform data conversions between reference types in C#. You can convert references from one type to another, but the reference types must be related.

After reading the upcoming posts, you should be able to:

·           Identify permitted and prohibited conversions between reference types.

·           Use conversion mechanisms (casts, is, and as).

·           Identify special considerations for conversion to and from the object type.

·           Use the reflection mechanism, which allows examination of run-time type information.

·           Perform automatic conversions (boxing and unboxing) between value types and reference types.

Converting Value Types

C# supports implicit and explicit data conversions.

Implicit Conversions

For value types, I have previously described two ways to convert data: implicit conversion and explicit conversion using the cast operator.

Implicit conversion occurs when a value of one type is assigned to another type. C# only allows implicit conversion for certain combinations of types, typically when the first value can be converted to the second without any data loss. The following example shows how data is converted implicitly from int to long:

int a = 4;
long b;
b = a; // Implicit conversion of int to long

Explicit Conversions

You can explicitly convert value types by using the cast operator, as shown:

int a;
long b = 7;
a = (int) b;

Exceptions

When you use the cast operator, you should be aware that problems might occur if the value cannot be held in the target variable. If a problem is detected during an explicit conversion (such as trying to fit the value 9,999,999,999 into an int variable), C# might raise an exception (in this case, the OverflowException). If you want, you can catch this exception by using try and catch, as shown:

try {
   a = checked((int) b);
}
catch (Exception) {
   Console.WriteLine("Problem in cast");
}

For operations that involve integers, use the checked keyword or compile with the appropriate compiler settings, otherwise checking will not be performed.

System.Convert Class

Conversions between the different base types (such as int, long, and bool) are handled within the .NET Framework by the System.Convert class.

You do not usually need to make calls to methods of System.Convert. The compiler handles these calls automatically.