Behind the Scenes Look at C#: Type Conversions continued - Implicit and Explicit Reference-Types Conversion
(Page 2 of 4 )
In this section we will discuss the implicit and explicit reference-type conversions in the same class inheritance hierarchy. We are not talking about implicit and explicit user defined type conversions between different classes in different class inheritance hierarchies. The C# compiler knows what to do when you perform explicit type conversion with classes in the same inheritance hierarchy (as we have seen in the previous article in the section "Introduction to Type Conversion, The Reference-Type Example"). The C# compiler also knows what to do when it needs to perform implicit type conversion with classes in the same inheritance hierarchy -- but it doesn't know how to perform implicit and explicit type conversions with classes in different class inheritance hierarchies. That's why C# features a syntax to do this job, as we will see later in this article. For now we will discuss implicit conversions in the same inheritance hierarchy.
Although implicit and explicit type conversions in the same inheritance hierarchy are related to the concepts of inheritance, I think that this is the best place to discuss it, because it's more related to casting and how object references are related to objects in the runtime.
If you remember the example of the section "Introduction to Type Conversion, The Reference-Type Example," you will recall that we had an object of type Worker (called worker1) and we cast the reference worker1 to a reference of type Person. In the next application you will understand the whole implicit and explicit reference-type conversion story. Let's take a look at the code first:
using System;
namespace TypeConversion
{
class Class1
{
static void Main(string[] args)
{
Worker worker1 = new Worker();
worker1.FirstName = "Michael";
worker1.LastName = "Youssef";
worker1.Department = "IT";
Console.WriteLine(worker1);
Console.WriteLine();
Person aPerson = worker1; // this doesn't require a cast
Console.WriteLine("this is aPerson = {0}", aPerson);
Worker worker2 = (Worker)aPerson; // this requires a cast operation
Console.WriteLine("this is worker2 = {0}", worker2);
Console.ReadLine();
}
}
class Person
{
private string firstName;
private string lastName;
public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
}
}
public string LastName
{
get
{
return this.lastName;
}
set
{
this.lastName = value;
}
}
public override string ToString()
{
return this.firstName +" "+ this.lastName;
}
}
class Worker : Person
{
private string department;
public string Department
{
get
{
return this.department;
}
set
{
this.department = value;
}
}
public override string ToString()
{
return this.FirstName +" "+ this.LastName+","+this.department;
}
}
}
Run the application after you compile it and you will get the following result to the console window.

Let's review the implicit and explicit operations involved when converting from one value-type to another. Implicit conversion means that there's no loss of data; the explicit conversion is needed because there might be some loss of data. The case of reference-types is different. As you can see, the object is heap-based. The implicit and explicit cast operations are performed on the object reference itself, and the result of running the application proves that because the same object values are printed to the console window using the object ToString() method.
We begin the Main method with a declaration of an object reference of type Worker and then assign values to the FirstName, LastName and Department properties. Then, we call the ToString method to print these values to the console window. After that we declare an object reference of type Person (aPerson) and assign the worker1 to it, which assigns the object worker1 to the aPerson reference.
Actually I did use the cast operator in the first part of the article, only to illustrate that casting is needed when you assign a value to another type that stores a range of values that is less than the value's type. Now you understand that with the reference type it's all about the reference object itself (unlike the value types, which simply hold the values on the stack).
So the statement Person aPerson = worker1; doesn't need casting, because here there's no lose of data (because it's heap-based). However, we need an explicit cast operation when we use this statement: Worker worker2 = (Worker)aPerson;. Because we know that aPerson refers to an instance of type Worker, we can cast to type Worker with no problems; again, the object is heap-based and we are dealing only with the object reference.
If the object reference aPerson refers to an object of type Person, then the statement Worker worker2 = (Worker)aPerson; will fail. It will generate the exception System.InvalidCastException because a Worker object contains more members than a Person object. We can't cast that way, as we get a exception of type System.OverflowException when we assign a value that exceeds the maximum range for the value type (when runs it a checked block).
So with reference types there are down-cast and up-cast conversion operations on object references. When you cast an object reference of a derived class type to an object reference of a base class type, then it's called up-cast, and as you know it doesn't need explicit conversion. The down-cast operation works the other way; when you cast an object reference of a base class to an object reference of a derived class, the operation is called a down-cast.
Next: User Defined Type Conversions >>
More C# Articles
More By Michael Youssef