Monday, April 25, 2005

Implicit Casting / Explicit Casting / "as" Operator / "is" Operator

Let see them in action :

1. Implicit Casting

When programming, it is quite common to cast an object from one data type to another. In this section, I'll look at the rules that govern how objects are cast between data types. To start, look at the following line:

System.Object o = new Jeff("ConstructorParam1");

The previous line of code compiles and executes correctly because there is an implied cast. The new operator returns a reference to a Jeff type, but o is a reference to a System.Object type. Since all types (including the Jeff type) can be cast to System.Object, the implied cast is successful.

2. Explicit Casting

However, if you execute the following line, you get a compiler error since the compiler does not provide an implicit cast from a base type to a derived type.

Jeff j = o;

To get the command to compile, you must insert an explicit cast, as follows:

Jeff j = (Jeff) o;

Now the code compiles and executes successfully.
Let's look at another example:

System.Object o = new System.Object();
Jeff j = (Jeff) o;

On the first line, I have created an object of type System.Object. On the second line, I am attempting to convert a reference of type System.Object to a reference of type Jeff. Both lines of code compile just fine. However, when executed, the second line generates an InvalidCastException exception, which if not caught, forces the application to terminate.
When the second line of code executes, the common language runtime verifies that the object referred to by o is in fact an object of type Jeff (or any type derived from type Jeff). If so, the common language runtime allows the cast. However, if the object referenced by o has no relationship to Jeff, or is a base class of Jeff, then the common language runtime prevents the unsafe cast and raises the InvalidCastException exception.

3. as Operator

C# offers another way to perform a cast using the as operator:

Jeff j = new Jeff(); // Create a new Jeff object
System.Object o = j as System.Object; // Casts j to an object
// o now refers to the Jeff object

The as operator attempts to cast an object to the specified type. However, unlike normal casting, the as operator will never throw an exception. Instead, if the object's type cannot be cast successfully, then the result is null. When the ill-cast reference is used, a NullReferenceException exception will be thrown. The following code demonstrates this concept.

System.Object o = new System.Object(); //Creates a new Object object
Jeff j = o as Jeff; //Casts o to a Jeff
// The cast above fails: no exception is raised but j is set to null

j.ToString(); // Accessing j generates a NullReferenceException

Here comes the Final Rescue !!

4. is Operator

In addition to the as operator, C# also offers an is operator. The is operator checks whether an object instance is compatible with a given type and the result of the evaluation is either True or False. The is operator will never raise an exception.

System.Object o = new System.Object();
System.Boolean b1 = (o is System.Object); // b1 is True
System.Boolean b2 = (o is Jeff); // b2 is False

Note, if the object reference is null, the is operator always returns False since there is no object available to check its type.

That's all i can say ...Happy Casting !!

Regards,
Deepak

No comments: