To convert one datatype to another in C#, use Type Conversion. In C#, the type conversions are of two types −
Implicit type conversion
Performed by C# in a type-safe manner. For example, are conversions from smaller to larger integral types and conversions from derived classes to base classes.
Explicit type conversion
Performed explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.
Let us see an example to cast double to int i.e. explicit type conversion −
Example
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
double d = 8745.97;
int x;
x = (int)d;
Console.WriteLine(x);
Console.ReadKey();
}
}
}Output
8745