You can convert a value from one data type to another. For example, if you are converting a string to an integer, you’ll need to parse the value first. Here are a few different ways you can convert and parse values.
Why Do You Need to Convert or Parse Data?
An example of when you may need to parse data is if you’re requesting user input in a C# console application:
In this case, the user will enter an integer value, but the application will store it as a string by default. If you tried to use the string value in any calculations, you’d get a compilation error:
Another reason to convert data types is to access certain methods that are only available for a specific data type. For example, the DateTime struct contains a function that allows you to add minutes. If you were using a string instead, you would not have access to the method:
You may also want to convert a value to another data type if you needed to follow the data type structure of a class created in C# or a structure in C#.
How to Convert and Parse Different Data Types
You can parse different data types such as integers, doubles, boolean types, and datetime types.
Strings to Integers
To convert a string to an integer, use the Parse() method:
You can also parse the string into integers of different bit sizes, such as Int16, Int32, or Int64.
If you are using the Parse() method, make sure the string value represents an integer. If you try to parse a non-integer value such as “Hello”, you will receive a parsing error at runtime.
You can also use the TryParse() method to catch any exceptions that could occur when parsing:
To convert an integer back to a string, use the ToString() method:
Strings to Doubles
Parsing a string to a double is very similar to parsing one to an integer. Use the Parse() method from the Double class:
As with integers, you can also use the TryParse() method to catch any parsing errors that could occur:
To convert a double back to an integer, use the ToString() method:
Doubles to Integers
You can use casting to convert a double to an integer. Because an integer only stores a whole number, the conversion will remove the decimal places. For example, converting “12.34” will only store the value “12”.
You can also cast an integer back to a double:
Strings and Boolean Values
To convert a string to a boolean, use either the Parse() or ToBoolean() method:
To convert a boolean back to a string, Use the ToString() method:
Integers and Boolean Values
To convert an integer to a boolean, use the ToBoolean() method to convert either a “1” or “0” to its equivalent boolean value. The value “1” will convert to “true” while “0” converts to “false”:
You can also pass an integer value other than “1” or “0” to the ToBoolean() method. By default, the method will still return “true”:
To convert a boolean back to an integer, use the Convert.ToInt32() method. Converting “true” will return a “1”, and converting “false” will return a “0”.
Strings to DateTime
To parse a string to datetime format, use the DateTime.Parse() method:
To convert a datetime back to a string, use the ToString() method:
Parsing Values to Different Data Types
Now you understand how to convert values from one data type to another. These are not the only conversions out there, so feel free to learn more about other data types in C#.
You can also explore how other data types work in other languages, such as JavaScript.