Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Conversion Operator Overloading in C#

1. Conversion Operator Introduction

Conversion Operator in C# converts one Type to another type. There are two flavours to it and we should implement both. One is Implicit Conversion and other one is Explicit Conversion. In a class type, we can provide two conversion operators those tells how a class can be casted to other type & how other type can be converted to class type. In this example, we will create a class called TimePart and add a conversion method to convert that to a String. We will also provide a conversion method which converts the String into a TimePart instance.

2. TimePart Members

The below code shows the TimePart class and its members. We also have a constructor to initialize.

Fig 1. TimePart Class Constructor
Fig 1. TimePart Class Constructor


  1. The class contains three private integers to represent the time parts Hour, Minute and Seconds.
  2. The constructor takes three integers and initializes the internal time-part members.

3. Getter Methods

In the below code, we have public methods to access the private TimePart members. Note, the constructor is the only way to initialize the member in our example. We don’t have any setter method in our example.

Fig 2. Getter Methods
Fig 2. Getter Methods

4. ToString Override

We need a way to represent our TimePart class in string format. Below is the ToString override.

Fig 3. ToString Method
Fig 3. ToString Method


  1. The method we are overriding is the ToString and the keyword override denotes it. The ToString method is called whenever we want to print our class in the string format. For example, we can append our class with a String using the ‘+’ operator and print that in console output window.
  2. Our ToString override separates the internal members with a ‘:’ in between. Here we use Format function of the string and provide place holder substitute like {0},{1} and so on.
  3. The place holders are filled by the Hour, Min, and Sec private members. So, our to string represents the TimePart in Hour:Min:Seconds string format.

5. Implicit Conversion Operator

Our TimePart can be converted to string and below is the conversion method.

Fig 4. C# Implicit Conversion Operator Overloading
Fig 4. C# Implicit Conversion Operator Overloading


  1. Conversion methods should be static and hence we marked the method as static followed by the implicit operator keywords. The implicit keyword here enables the implicit conversion like String str = time_part_instance;.
  2. We want to convert TimePart as a String and hence the parameter type is TimePart and return type is String.
  3. Before we do the conversion, we set return value as empty.
  4. The Format function reads the internal members and assigns that to the return string.

6. Explicit Conversion Operator

With explicit conversion operator, we can assign a String to a TimePart instance. The conversion function is shown below:

Fig 5. C# Explicit Conversion Operator


  1. The keyword explicit operator says that we are defining the function for explicit conversion.
  2. Since the conversion is from String to TimePart, the function takes String as a parameter and returns TimePart to the caller.
  3. The string should be in the HH:MM:SS format. So, inside the body of the operator, we make a call to Split function to have an array of the by-parted string.
  4. As we need to return the TimePart, we construct the instance of a TimePart.
  5. The by-parted string array contains Hour, Min and Seconds and we store that in the TimePart instance’s internal members.
  6. The TimePart instance is returned to the caller. The returned object is a converted instance of TimePart.

7. Using the Conversion Operators

The code below tests how our conversion operators are working.

Fig 6. Using C# Overloaded Conversion Operators
Fig 6. Using C# Overloaded Conversion Operators


  1. First, we create the TimePart instance to denote 6 hours, 40 minutes, and 20 seconds. The internal members of the tp instance holds these time values.
  2. Here, we assign the TimePart instance to a String variable StrTimeParts. Since, we have implicit conversion operator, we will get the time compartments converted into string as return value which will be assigned to StrTimeParts. Here, the tp will go as a parameter to our implicit conversion operator.
  3. The converted value is printed and shown in the console output window.
  4. Next, we store a time in string format in the variable called time.
  5. The statement (TimePart)time performs the explicit conversion. Here, we attempt to convert a String into TimePart, and it will succeed as we have explicit conversion operator. We know that our explicit conversion operator by-parts the passed-in string and assigns that to the internal members of the TimePart. This code snippet stores the return value in a reference called tp2.
  6. We print the TimePart instance in the console window. Note, the statement here does not call the ToString function as we have a implicit conversion function. The string concatenation here calls our implicit conversion function and appends the return value with the constant string.
  7. Since the Time formatted string is converted as TimePart, we can easily read the required time part(s) and display it. In our case, we displayed only hours and seconds.

8. Code Reference

8.1 TimePart.cs

8.2 Program.cs

Categories: C#

Tags: , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.