Sunday 1 August 2021

Type Conversion(Casting) in C#

Type Conversion(Casting) in C# is converting one type of data to another type. It is also known as Type Casting. Type casting has two forms − 

Implicit type conversion − These conversions are 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 − These conversions are done explicitly by users using the pre-defined functions. 
Explicit conversions require a cast operator. 

Example: 
double d = 5673.74; 
 int i; 
 // cast double to int. 
 i = (int)d;

few more keywords for type casting:
ToBoolean()
ToString()
ToInt32()
ToDouble()
ToDateTime()

1 comment: