Converting value with Convert.ChangeType in C#

Sometimes we need to convert one type to another. In some cases we may not know the type of the value that is going to be converted.

The following code shows the use of Convert.ChangeType method. Notice that the object boxedValue has a string value. As it’s an object, we don’t know the type. So while converting we provide the type of the target type.


The following are the four overloaded methods. The TypeCode is an enum type, which has all types.


Let’s debug the code. The first converted value was successfully converted to int type.


And it shows the type of the value.


Similarly the double type is successfully achieved with the target type of the converted value as double as well.



Hope this post is helpful.

Thanks for reading. Like Method Of Tried in Facebook.