find and solve || findandsolve.com
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.

Example
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:

Example
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):

Example
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;
                Console.WriteLine(i);
                Console.ReadKey();
            }
      }
}

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
1ToBoolean
Converts a type to a Boolean value, where possible.

2ToByte
Converts a type to a byte

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

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

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

6ToDouble
Converts a type to a double type.

7ToInt16
Converts a type to a 16-bit integer.

8ToInt32
Converts a type to a 32-bit integer.

10ToInt64
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(i.ToString());
         Console.WriteLine(f.ToString());          Console.WriteLine(d.ToString());
         Console.WriteLine(b.ToString());
         Console.ReadKey();
      }    }
}

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

70

32.003
234567.765223
True