User-Defined Conversion

In this article, I am going to share with you about User-Defined Conversion in C#.

User-Defined Conversion
 
In C# programming, a situation may arise when we have to convert a data type into our own custom type or vice-versa. The custom type may be any class, object, or struct that we created. To perform these types of special conversions, we have to define a special method. The syntax of these special methods is shown below.
  1. static public implicit/explicit operator ConvertToType(ConvertFromDataType value)  
  2. {  
  3.     // Conversion implementation and return type of this method must be 'ConvertToType'  
  4.     return ConvertToType;  
  5. }  
Here, in the above method, 'ConvertFromDataType' is the data type we are going to change and 'ConvertToType' will be the converted type. We use implicit or explicit depending on the situation. The return type of this method should be of Converted type. Understand the below example to have more clarity around it.
  1. using System;  
  2.   
  3. namespace Tutpoint  
  4. {  
  5.     class Program  
  6.     {  
  7.         class Student  
  8.         {  
  9.             public string Name { getset; }  
  10.             public int Roll_No { getset; }  
  11.   
  12.             // This method used to convert int value into 'Student' type  
  13.             static public implicit operator Student(int value)  
  14.             {  
  15.                 // Return type of this method should be of 'Student' type  
  16.                 return new Student { Name = "jainy", Roll_No = value };  
  17.             }  
  18.   
  19.             // This method used to convert 'Student' type value into 'int' type  
  20.             static public explicit operator int(Student student)  
  21.             {  
  22.                 // Return type of this method should be of 'int' type  
  23.                 return student.Roll_No;  
  24.             }  
  25.         }  
  26.   
  27.         static void Main(string[] args)  
  28.         {  
  29.             // Object of Student class is initialised  
  30.             Student student = new Student();  
  31.             int value = 100;  
  32.   
  33.             // On below line, int value is converted to student type, implicit  
  34.             student = value;  
  35.             Console.WriteLine("Student name" + student.Name + " Student Roll No." + student.Roll_No);  
  36.   
  37.             // On below line, value is assigned from student.Roll_No  
  38.             value = student.Roll_No;  
  39.             Console.WriteLine("Roll No. " + value);  
  40.   
  41.             // On below line, student type is converted to int type  
  42.             value = (int)student;  
  43.             Console.WriteLine("Roll No. " + value);  
  44.             Console.ReadKey();  
  45.         }  
  46.     }  
  47. }  
In this example, we have created a class named 'Student' which contains two property members 'Name' and 'Roll_No'. We have also written two special conversion methods to convert from type 'Student' to type 'int' and vice-versa.
 
The conversion from "int" to "Student" can be performed directly. As we are converting from smaller to larger data types, so we use implicit on method definition, while the conversion from 'Student' to 'int' requires a cast operator as we are converting from larger to smaller type and we use explicit on method definition.
 
In 'Main' method, we have directly assigned an int value to Student type without compiling for errors due to the method defined above otherwise, an error will produce as "Cannot implicitly convert type 'int' to 'Tutpoint.Program.Student'". The same happens for conversion from "student" to "int" type.
Ebook Download
View all
Learn
View all