C#  

Type Conversion in C#

Type Conversion

Conversion of one type of variable into another type of variable is called casting, as in the following:

int i = 10;
float k = 20.0f;

k = i; // it is possible
i = k; // not possible
i = (int)k; //again possible

Error in Casting



I am dividing this article into two parts, in the first part I'll explain why we can't convert an int into a short directly but the opposite is possible and in the second part I'll explain the other data type, float, by comparing it with int.

Part: 1 [int VS short]

As I said above, an int cannot be directly converted into a short but the opposite is possible. But I will tell you in this article how can you convert an int into a short too.

Explanation

int i = 10;
short j = 5;

i = j; // possible
j = i; // not possible

Now the question is how to convert int into short, so here we go-

int i = 10;
short j = 5;

i = j; // possible
j = (short)i; // now possible

Example

Snippet: 1.1

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
Hello_Word
{

   class
Program
   {

      static
void Main(string[] args)
      {

         int
i = 10;
         short
j = 5;
         float
k = 20.0f;
         i = j;
         j = (
short)i;
         Console
.WriteLine("value is: " + j);
         k = i;

         // i = k;

         Console
.ReadLine();
      }
   }
}



Now I will show you what changes occur whenever we convert a large data type into a smaller data type.
(In this case it is for an int to a short.)

Snippet: 1.2

using System;  
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
Hello_Word
{

   class
Program
   {

      static
void Main(string[] args)
      {

         int
i = 100000;
         short
j = 5;
         float
k = 20.0f;
         // i = j;

         j = (
short)i;
         Console
.WriteLine("value is: " + j);
         k = i;

         // i = k;

         Console
.ReadLine();
      }
   }
}


Reason

This variation in input is due to the size of the int and short data types. An int size is 32 bits while a short is only 16 bits. Thus the input in the first case is 5 and in the second case due to the limited size, the short loses some memory from the data and shows 31072 as output.

Part: 2 [int VS float]

Type conversion between int and float data types.

Explanation

int i = 10;
float k = 20.0f;

k = i; // possible
i = k; // not possible

Now the question is, how to convert an int into a short, so here we go.

int i = 10;
float
k = 20.0f;

k = i; // possible
i = (int)k; // possible

Example

Snippet: 2.1

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
Hello_Word
{

   class
Program
   {

      static
void Main(string[] args)
      {

         int
i = 10;
         // short j = 5;

         float
k = 20.0f;
         k = i;
         i = (
int)k;
         Console
.WriteLine("value is: " + i);
         Console
.ReadLine();
      }
   }
}


Snippet: 2.2

using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
Hello_Word
{

   class
Program
   {

      static
void Main(string[] args)
      {

         int
i = 10;
         // short j = 5;

         float
k = 20.0f;
         // k = i;

         i = (
int)k;
         Console
.WriteLine("value is: " + i);
         Console
.ReadLine();
      }
   }
}


Reason

This variation in input is due to the size of the int and float data types. An int size is 32 bits while a float is large in comparison to an int (64 bits). Thus the input in the first case is 10 and in the second case it's showing 20 as the output.