C# Type Casting

We need to convert data of one type to another type before it is used in arithmetic operations that are called Type Conversion or Type Casting. In C#, type casting is two types:

  • Implicit Conversions
  • Explicit Conversions

Implicit Conversions:

Implicit Conversions can always be performed without any loss of data.
Example:

short y=50;
int x=y; // Implicit Conversions

Explicit Conversions:

Many conversions can’t be implicitly made between types. If we attempt such conversions, the compiler will give an error message. We can explicitly carry out such conversions using the ‘cast’ operator. This process is known as Explicit Conversions. It has the following syntax:

type variable1=(type) variable2;

Example:

float x=3.14;
int y=(int)x;