C# Convert Class

Çağlar Can SARIKAYA
3 min readApr 26, 2022

--

Figure 1. Funny Converter Class

How does the convert method works in the C# program language, when you convert from string to double, how does it knows to split groups and decimal part of numbers?

Of course, it can not convert if the input value is not suitable for the output type. Convert works like a pipe and it has one input and one output, like a clarinet. The air goes in then some process then notes are going out.

Convert class creates a new object as you want and puts it at the end of a pipe, and puts your value at the beginning of the pipe. Let's look into how the process works. It will get the type of your destination, if you try to convert something to double, this is your destination type. There are some rules between types, so it will check them and accept or decline the conversion process.

If you would like to check these rules you can find them in IConvertible this is an interface that describes rules between variables

If you tried from an integer to double it allows you to do that easily because you can keep an integer value in a 4-byte place and a double place is an 8-byte, so first is you are trying to put the small thing into a big thing its ok also you will change the types it was the natural number but after this turns to a rational number. So you know all natural numbers are also rational numbers

Figure 2. C# data types

What happens if the destination is smaller than the source? It overflows. You can say ok in that scenario, yes I know, keep how much you can put in but it will warn you.

The other thing that I would like to explain a little bit, What happens when you convert a number in the string to double?

When your c# program starts to run, it gives culture to define some system behaviors. One of them is converting you can specify this by giving IFormatProvider.

Figure 3. Converting Culture Example

Note: System culture is thread-specific. You will not face to face this at kind of these things, but you should know, If you want to specify your system culture, you should be careful because other threads are still in default culture. You can get some errors with it.

Happy Code :)

--

--

Çağlar Can SARIKAYA
Çağlar Can SARIKAYA

No responses yet