find and solve ||
Please wait.....

Casting and type conversions in C#

Type casting is when you assign a value of one data type to another type.

In C#, there are two types of casting:

  • Implicit Casting (automatically) – converting a smaller type to a larger type size and conversion from derived classes to base classes.

char-> int-> long-> float-> double

  • Explicit Casting (manually) – converting a larger type to a smaller size type. It requires a cast operator.

double-> float-> long-> int-> char

Implicit casting is done automatically when smaller size type is passed to a larger size type.

int  myInt = 7;
double myDouble = myInt; // Automatic casting: int to double
Console.WriteLine(myInt); // Outputs 7
Console.WriteLiine(myDouble); // Outputs 7

Explicit Casting

It must be done manually by placing the type in parentheses in front of the value:

double myDouble = 8.65;
int myInt = (int) myDouble; // Manual casting: double to int
Console.WriteLine(myDouble); // Outputs will be 8.65
Console.WriteLine(myInt); // Outputs will be 8

Type Conversion Methods

It is also possible to convert data types explicitly by using built- in methods, such as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) and Convert.ToInt64 (long):

int  myInt = 9;
double myDouble = 6.25;
bool myBool = true;

Console.WriteLine(Convert. ToString(myInt)); // convert int to string Console. WriteLine(Convert. ToDouble(myInt)); // convert int to double Console. WriteLine(Convert. ToInt32(myDouble));// convert double to int Console.WriteLine(Convert. ToString(myBool)); // convert bool to string

Type Conversion is also known as Type Casting.

The following example shows an explicit type conversion:

using System;
namespace TypeConversionApplication {
         class  ExplicitConversion  {
              static void Main (string [] args)  {
                  double d  = 56570.71;
                  int i;
                 // cast double to int.	
                 I = (int) d;

When the above code is compiled and executed, it produces result 56570.

C# Type Conversion Methods

C# provides the following built-in type conversion methods −

Sr.No.Methods & Description
Converts a type to a Boolean value, where possible.

Converts a type to a byte

Converts a type to a single Unicode character, where possible.

Converts a type (integer or string type) to date-time structures.

Converts a floating point or integer type to a decimal type.

Converts a type to a double type.

Converts a type to a 16-bit integer.

Converts a type to a 32-bit integer.

Converts a type to a 64-bit integer.

The following example converts various value types to string type 

using System;
namespace TypeConversionApplication {
   class StringConversion {
      static void Main(string[] args) {
         int i = 70;
         float f = 32.003f;
         double d = 234567.765223;
         bool b = true;
         Console.WriteLine(f.ToString());          Console.WriteLine(d.ToString());
      }    }

When the above code is compiled and executed, it produces the following result −