In C#, data can be converted from one type to another using either implicit conversion or explicit conversion. Refer to the following article by Mytour for detailed insights into various ways of data type conversion in C#.
Data Type Conversion in C#
1. Data Type Conversion in C#.
2. Implicit Type Conversion.
3. Explicit Type Conversion.
4. Using the Convert Class.
1. Data Type Conversion in C#
As mentioned earlier by Mytour, there are two ways to convert data types in C#: implicit type conversion or explicit type conversion. Below, Mytour will delve into details introducing each method of data type conversion in C#.
2. Implicit Type Conversion
Various data can be compiled using different data types. For example, the number 74 can be interpreted as an integer type but can also be understood as a double type (74.0). There are 2 scenarios if implicit type conversion is applied.
The first scenario occurs when we compute an expression. The compiler automatically adjusts the data types we use in that expression:
In the code snippet above, variable b is of type double and variable x is of type int. In the expression b + x, the compiler implicitly converts x from int data type to double data type, then assigns the result to b.
The second scenario that may occur is the compiler storing the result in a variable:
In the example above, we can observe that both x and y are of int data type, but the returned result is of double data type.
Additionally, you can refer to another example below:
Normally, we would assume that the result in the example above is 4.2. However, this result is entirely incorrect.
The compiler calculates the expression on the right-hand side first, then converts the result to double data type.
The expression x/y contains integer type, so the returned result is an integer, in this example the returned value will be 4 (rounded value). After computation, the compiler will convert the result to double type and assign the value to variable b:
Additionally, you can fix the error in the expression above by using explicit type conversion on variable x or y in the expression.
3. Explicit Conversion
For explicit type conversion, we need to write additional code to convert from one data type to another. To achieve this, you can apply the cast operator or the Convert class.
To better understand explicit type conversion, readers can refer to the example below:
The compiler will report an invalid conversion. What we are missing is the cast operator:
By using conversion (int), we can convert safe data types and the compiler will accept this.
The returned result is accurate. The important thing to note is that the cast operator may truncate data when converting from a larger value range data type to a smaller value range data type. For example, when converting from double data type to int data type.
Now you can apply the cast operator in the above example from the implicit type conversion part to get the accurate result:
The accurate result returned is 4.2.
4. Using the Convert Class
As mentioned above by Mytour, we can use the Convert class with its static methods to convert specified data type to another data type:
Thus, the article above has just introduced you to how to convert data types in C#. If you have any questions or need clarification, readers can leave their comments below the article, and Mytour will answer your questions as soon as possible. Additionally, readers can refer to some other C# lessons on Mytour to learn more about Variables in C# as well as setting up environment for C#.