Type conversion is to convert one data type to another type. The two forms are -
Implicit Type Conversions - These conversions are performed by C# in a type-safe manner. For example, conversion from small to large integer types and conversion from derived classes to base classes.
Explicit Type Conversions - These conversions are functions done explicitly by the user using predefined types. Explicit conversion requires a cast operator.
The following are the built-in type conversion methods-
Sr.No | method And description |
---|---|
1 |
ToBoolean Convert the type to Boolean (if possible) . |
2 |
ToByte p> Convert type to bytes. |
3 |
ToChar Convert the type to a single Unicode character if possible. |
4 |
ToDateTime Convert type (integer or string type) to date Time structure. | tr>
5 |
ToDecimal Convert a floating point or integer type to a decimal type. p> |
6 |
ToDouble Conversion type to double type. |
The following example converts an integer to a string type-
Live demonstration
using System; namespace Demo { class Program { static void Main(string[] args) { int i = 20; Console.WriteLine(i.ToString()); Console.ReadKey(); } } }
20
The above is the detailed content of What is type conversion in C#?. For more information, please follow other related articles on the PHP Chinese website!