Behind the Scenes Look at C#: Type Conversions - The Reference-Type Example
(Page 3 of 6 )
In this example I use inheritance to form a hierarchy of C# classes. Inheritance will be discussed in the series shortly; for now just understand that C# permits only single inheritance from a base class and permits multiple inheritance from interfaces. You use the : operator after the class identifier declaration to form the inheritance.
In this example, the class Person defines two properties with two private fields and a ToString() method. The class Worker inherits from the class Person. It adds the Department property and the private field department in addition to overriding the base class ToString method (in this case the base class here is the Person class). The example addresses the difference in casting between value-types and reference-types.
using System;
namespace TypeConversion
{
class Class1
{
static void Main(string[] args)
{
Worker worker1 = new Worker();
worker1.FirstName = "Mary";
worker1.LastName = "John";
worker1.Department = "IT";
Console.WriteLine("The worker1 object ToString() method prints");
Console.WriteLine(worker1.ToString());
Console.WriteLine();
Person someone = (Person)worker1;
Console.WriteLine("The cast operator has been used with the worker1 object");
Console.WriteLine("The someone object ToString() method prints");
Console.WriteLine(someone.ToString());
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;
}
}
}
The result to the console window is:

As you can see, the result is the same (Mary John, IT) even when we assign the worker1 object to a reference of type Person, but the interesting issue here is when you use the . operator to access the members of each object reference. Let's take a look, when you use the dot operator (.) on the worker1 object you will get the following list:

As you can see, the Department property is shown with the other public members of the class. But when we cast the worker1 object to a reference of the base class (Person), we have limited our visibility of the object members to the members of the base class. That's what we will see when we use the dot operator (.) with the instance someone:

The Department is not shown in the list because of the casting operation, but we didn't lose this piece of data. The ToString() method can prove that, because it has printed the same string that contains the IT character value. So with value-types we lose data with casting because it's a copying operation; we copy bits from one type's variable to another.
The situation is different with reference-types; we have the object in the managed heap (unlike the value-types, where the instance is stack-based). When we do the cast operation we just limit the visibility of the object to the object that we are casting to (the base class). I think that now you have a clear vision of the whole casting issue, so let's discuss implicit and explicit type conversions with both value-types and reference-types.
Next: Implicit Type Conversions (Built-In Value-Types) >>
More C# Articles
More By Michael Youssef