Behind the Scenes Look at C#: Type Conversions continued - User Defined Type Conversions
(Page 3 of 4 )
When the C# compiler needs to perform an implicit conversion on primitive data types, it knows exactly what it needs and the operations it will perform; it also knows what to do with the explicit cast operations. The same thing applies to the classes that exist in the same class inheritance hierarchy; the C# compiler knows what to do when it needs to perform an implicit or an explicit conversion operation. What if we need to convert from a type to another one that doesn't exist in the same class inheritance hierarchy? C# offers a syntax that's similar to operator overloading that can be used when you need to convert (implicitly or explicitly) from one type to another.
The syntax that C# offers for user defined type conversions can be used to state an implicit type conversion between two classes (or structures) or an explicit type conversion. Of course we have to simulate the compiler's behavior. In other words, we use the cast operator when we convert a double variable to an int because we know that we may lose some data. The C# compiler performs an implicit type conversion when the operation is saved, and it will not lose any data.
The same point applies to user defined conversions. When we are sure that the conversion operation from one type to another type is saved, we will use the syntax that defines an implicit conversion. If the operation can cause some data to be lost, however, we will use the syntax of the explicit conversion (for which the cast operator must be used) to state that. The user defined type conversions can be defined and used with classes as well as structures, and the syntax is the same. Note that you can use the C# user defined type conversion with classes in the same class inheritance hierarchy, but not between a derived type and a base type; you can use it between two classes that participate in the same base class.
For example, you have the classes Customer and Worker, which both inherit from the class Person. In this class inheritance hierarchy you can't define user defined type conversions between the Customer class and its base class (Person); you also can't perform the operation between the Worker class and its base class (Person) because they are in the same inheritance hierarchy, and the compiler can perform implicit and explicit casting operations. The only valid user defined type conversion here is between the Customer class and the Worker class. Let's see that in an example:
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();
Customer customer1 = new Customer();
customer1.FirstName = "Mary";
customer1.LastName = "Jerry";
customer1.OrderID = "123456789";
Console.WriteLine(customer1);
Console.WriteLine();
Console.WriteLine("casting the worker1 object to customer1");
customer1 = (Customer)worker1;
Console.WriteLine();
Console.WriteLine("the object customer1 after the casting");
Console.WriteLine();
Console.WriteLine(customer1);
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;
}
}
}
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;
}
public static explicit operator Customer(Worker work)
{
Customer temp = new Customer();
temp.FirstName = work.FirstName;
temp.LastName = work.LastName;
temp.OrderID = "000000000";
return temp;
}
}
class Customer: Person
{
private string orderID;
public string OrderID
{
get
{
return this.orderID;
}
set
{
this.orderID = value;
}
}
public override string ToString()
{
return this.FirstName +" "+ this.LastName+","+this.orderID;
}
}
}
Compile the code and run the application and you will get the following result to the console window:

Next: Explanation and One More Example >>
More C# Articles
More By Michael Youssef