Standard Type Casting and Conversions

Started by dhilipkumar, May 20, 2009, 05:04 PM

Previous topic - Next topic


Standard Type Casting and Conversions

C# has two primary ways of changing the types of objects from one type to another. There is the standard casting performed by naming the type to cast to before the expression to cast from inside of a set of parentheses. The other way is to use the as operator.

Basic Type Casting

object myIntObj = 1;
int myInt = (int)myIntObj;
Listing 2: Basic Use of the "as" Operator
object myStringObj = "Brendan Enrick";
string myString = myStringObj as string;

I am sure pretty everyone has seen both of these, but something more important than just knowing about them is knowing what they do and when to use each one. The goal of this article is to explain when each should be used and why.

Understanding the Basics

Simple type casting is pretty well understood. You change the type of an object at runtime from one type to another. The common scenario is to convert from a less specific to a more specific type. For example, we might be converting from an object to an integer.

If our current instance of type object is not really an integer or anything that can safely cast to an integer we will throw an exception. This can be a good or a bad outcome for our program. It is good that the program does not continue executing with incorrect data, but the program either crashed or handled the exception.

Note: If you are trying to use the "as" operator on a value type object you will need to use a nullable value type. Read the following references for more info on these.



When to Use Each Form of Conversion

I have found it to be a good idea to use the casting operator when it should be unlikely for the casting to fail. This way if there is a problem we definitely know about the error and handle the situation. When deciding whether to catch or check for exceptions, it is important to consider the likelihood of the error occurring.

Since the "as" operator is able to handle things safely, it should generally be used when you will not be certain if the value of the object to be converted will cast correctly.

"as" Operator Equivalent Code
expression is type ? (type)expression : (type)null

The "expression" there could be any expression we were using to cast as a different type, so for the simplicity of the previous explanation it is the original object we are casting to the new type.

Using the "as" Operator

Checking For Null after Conversion

MyType converted = myObj as MyType;
if (converted != null)
  // work with the object here
  // we know the conversion worked.

MyType is a reference type. By now you have probably noticed that I have been mentioning reference types for this example.


Standard Type Casting and  Conversions

As a simple example I will convert a double object to an integer using the technique I have described here.

Conversion Using Value Type Objects

object myObj = 0.0;
int? converted = myObj as int?;
if (converted != null)
  // work with the object here
  // we know the conversion worked.

Working with conversion of types in C# is not all that complicated. We have looked at standard casting as well as using the "as" operator to convert between types. Keep in mind the importance of reference types with the "as" operator, and try to only use it when there is a high likelihood that the conversion will not work or if the failure to convert is not an error.


Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview