Conversions in C# are everyday operations for most developers. We use it to verify inputs and solidify the flow of our applications. There are built-in conversions and supporting facilities in C# that allow you to maintain flexibility and consistency throughout your applications. By default, there are two main types of conversions: one is called implicit and the other is called explicit. C# also provides a way for you to define your own conversions, which are called user-defined conversions. By adhering to some rules, you are able to create your custom conversions that tell the compiler and the runtime how they should handle conversion between classes that you have created. First, we’ll take a look at implicit and explicit conversions and then turn our eyes toward the custom user-defined conversions.
We call a conversion implicit when the change is done without needing to use the conversion operators.
For example:
1float pi = 3.14f;
2double dpi = pi;
3Console.WriteLine($"pi: {pi},\ndpi: {dpi}");
Note how the value of the pi changes.
1pi: 3.14,
2dpi: 3.14000010490417
The \n character is a new-line character, which adds only that to the output, so we can see separate lines on our screen.
We call a conversion explicit when the change is done with the use of a conversion operator.
For example:
1double pi = 3.14;
2float fpi = (float)pi;
3Console.WriteLine($"pi: {pi},\nfpi: {fpi}");
Note how the length of the numbers did not change.
1pi: 3.14,
2fpi: 3.14
This is due to the fact that float and double use different resolutions or memory space to store their values. Simply put, they have different sizes. Conversion from double to float will result in the loss of information which you need to be aware of.
In order to implement custom conversion or cast, we need to be aware that there are two functions that allow us to do this and need to be implemented in our class if we want this to work. A user-defined conversion consists of an optional explicit conversion followed by either an implicit or explicit user-defined conversion operator, finalized by an optional standard explicit conversion. If you are interested, check out this awesome guide on MSDN which explains everything, in detail, about user-defined conversions and their steps.
The function responsible for the implicit conversion looks like this:
1static public implicit operator <class>(<type> value) {
2 return new <class>() { <value> = value, <boolean> = false };
3}
The function responsible for the explicit conversion looks like this:
1static public explicit operator <type>(<class> <instance>){
2 return <instance>.<value>;
3}
Let's define our custom class.
1using System;
2
3namespace UserDefined
4{
5 public class Conversions
6 {
7 public class CustomType {
8 public int Number { get; set; }
9 public bool magic { get; set; }
10 static public implicit operator CustomType(int value) {
11 return new CustomType() { Number = value, magic = false };
12 }
13 static public explicit operator int(CustomType magic)
14 {
15 return magic.Number;
16 }
17 }
18 static void Main(String[] args)
19 {
20 int aNumber = 3;
21 CustomType magic = aNumber;
22 Console.WriteLine($"From int: {aNumber},\nto: {magic},\nWith value: {magic.Number}");
23 Console.Read();
24 }
25 }
26}
Calling the code shows us how the conversion goes from one type to another.
1From int: 3,
2to: UserDefined.Conversions+CustomType,
3With value: 3
By adding this little code we can easily backward convert.
1int bNumber = (int)magic;
2Console.WriteLine($"From: {magical},\nWith value: {magical.Number},\nto: {bNumber}");
This results in the following output.
1From: UserDefined.Conversions+CustomType,
2With value: 3,
3to: 3
As you can see, these two functions are all that is needed to convert between types, however, this is a fairly simple example and it can be further improved upon. The overall conversion is defined by your app requirements and what you need to implement in those two functions.
Starting from implicit conversions, throughout explicit conversions we build up our guide towards the user-defined conversions. These are widely used conversions in legacy corporate applications that support infrastructure or business logic. Basically, with two simple functions, you are able to create your custom class for conversion and C# allows you to define your own business logic to implement that. I hope you have found what you were looking for and it was worth your while. Thanks for reading.