Type Casting in C#


What is Type Casting?

Converting one data type value to another data type is called type casting. It is two types

  • Implicit Casting(Automatic)
  • Explicit Casting(Manual)


IMPLICIT CONVERSION

Automatically converts from one data type to another  without a programmer intervention

Compiler directly converts from smaller to larger size data type( char-int-long-float-double).

Sample Program for example:

EXPLICIT CONVERSION

Manually converting from one data type to another(double-float-long-int-char ) is called explicit casting. 

Sample Program for example:

Other available Explicit conversion available are

We have some inbuilt methods for Explicit conversion, which is given below with a clear example

Convert.ToInt32(int)
Convert.ToInt64(long)
Convert.ToBoolean,
Convert.ToDouble,
Convert.ToString.

 Sample Program for example:



 

Post a Comment

Previous Post Next Post

نموذج الاتصال