Operator Overloading With Implicit And Explicit Casts In C#

In C# programming, whenever we need to convert one type to another type, we need explicity convert and assign it to target type.

Example

We have two classes -  the entity class Employee and EmployeeViewModel class. Then, whenever we display employee data to View, we first convert the Employee type to EmployeeViewModel.

See below example.
  1. public partial class Employee {  
  2.     public int Id {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string Name {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string Address {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string PhoneNumber {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public System.DateTime CreatedDate {  
  19.         get;  
  20.         set;  
  21.     }  
  22. }  
  23. public class EmployeeViewModel {  
  24.     public int Id {  
  25.         get;  
  26.         set;  
  27.     }  
  28.     [Required]  
  29.     public string Name {  
  30.         get;  
  31.         set;  
  32.     }  
  33.     [Required]  
  34.     public string Address {  
  35.         get;  
  36.         set;  
  37.     }  
  38.     public string PhoneNumber {  
  39.         get;  
  40.         set;  
  41.     }  
  42.     public DateTime CreatedDate {  
  43.         get;  
  44.         set;  
  45.     }  
  46. }  
 
 
In the previous section, we converted the Employee object to EmployeeViewModel. For that, we first created EmployeeViewModel object and then assigned the respective property of Employee class to EmployeeViewModel. Then, we assigned the new EmployeeViewModel type to the target EmployeeViewModel type.
 
But here, the problem is that whenever we need this type of conversion, we can do the same as the previous one in all places. So, how to convert one type into another one and still reuse the code?
 
Well, in this case, we use Explicit/Implicit Casts Overloading.

Syntax
  1. public static explicit operator ConvertToDataType(ConvertFromDataType EmployeeEntity)  
  2. {  
  3.    return new ConvertToDataType;  
  4. }  
Example
  1. public static explicit operator EmployeeViewModel(Employee EmployeeEntity) {  
  2.     return new EmployeeViewModel {  
  3.         Id = EmployeeEntity.Id,  
  4.             Name = EmployeeEntity.Name,  
  5.             Address = EmployeeEntity.Address,  
  6.             PhoneNumber = EmployeeEntity.PhoneNumber,  
  7.             CreatedDate = EmployeeEntity.CreatedDate  
  8.     };  
  9. }  
 
 
 
In the above example, we saw how we can convert Employee type to EmployeeViewModel type using Explicit Cast Overloading.
 
We can do the same thing using Implicit Cast Operator Overloading too. See below - 
 
Syntax
  1. public static implicit operator ConvertToDataType(ConvertFromDataType EmployeeEntity)  
  2. {  
  3.    return new ConvertToDataType;  
  4. }  
Example
  1. public static implicit operator Employee(EmployeeViewModel EmployeeEntity) {  
  2.     return new Employee {  
  3.         Name = EmployeeEntity.Name,  
  4.             Address = EmployeeEntity.Address,  
  5.             PhoneNumber = EmployeeEntity.PhoneNumber,  
  6.             CreatedDate = EmployeeEntity.CreatedDate  
  7.     };  
  8. }  
 

Here, we can see how we convert EmployeeViewModel type to the Employee type using Implicit Cast Operator Overloading and reuse the logic everywhere when we need this type of conversion.
 
We can do this type of conversion with any type, like object, struct,class etc.
 
For more details, please refer to the below MSDN link.