Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - TYPE CASTING


So far we have seen, how to add two numbers that are of the same type.


i.e. We have added two numbers that are of Data Type Int.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
	int x = 5;
	int y = 6;
	int z = x + y;
	System.Console.WriteLine(z);
    }
} 


Output :



  The added value is : 11

So, if we see the above output, we got the correct added result (i.e. 11) because the Data Type of x and y is Int because both have an integer value in it.


int x = 5;
int y = 6;

Similarly, if we see the the variable z, it is also int. Because z holds the added value of the variables x and y. Which is also an integer(i.e. 11).


Now, there might also be a scenario where you want to add a floating point number and a whole number. And get the result as a whole number.


i.e. We want to add the whole number 5 and a floating point number 6.5. And the output we want should be 11 and not 11.5.


Let us see in the below example.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
	int x = 5;
	float y = 6.5F;
	int z = x + y;
	System.Console.WriteLine("The added value is : "+z);
    }
}


Output :



  error CS0266: Cannot implicitly convert type float to int. An explicit conversion exists (are you missing a cast?)

Well! If we see the output! It says, Cannot implicitly convert type float to int.


Which in simple words means, a floating point number (i.e. 6.5) and and a whole number (i.e. 5) when added cannot produce an Integer.


Now, if we see the Data Types of all the variables :


For variable x, it is int.For variable y, it is float.For variable z, it is int.


Which says, z cannot hold an Integer.


So, how do we solve this problem?


And this is where, Type Casting comes into picture.


Let us rewrite the above program with Type Casting.


Converting a Float Data Type to an Int Data Type


A solution to it would be, to convert the added value to Int.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
	int x = 5;
	float y = 6.5F;
	int z = (int)(x + y);
	System.Console.WriteLine("The added value is : "+z);
    }
}	 


Output :



  The added value is : 11

Now, if we see the output,


The added value is : 11

We can see the added value of 5 and 6.5 is 11 and it didn't result an error.


And how did we achieve this?


Well! By Type Casting.


Now, if we see the above code, we have assigned the number 5 to a variable x.


int x = 5;

And the floating point number 6.5 to variable y.


float y = 6.5F;

Now, we don't want the added result as 11.5. But we want only 11.


We have used type converter to convert the added value to Int.


int z = (int)(x + y);

We have simply used int before the added value x+y

C_Sharp

And now since the added value is converted to int Data Type, there is no problem. We get the output as 11.


The same rule applies to convert any other data types.


Now, let us say we want to rewrite the same example, with a little change.


i.e. In the above scenario, we have converted the float type value 11.5 to int type 11. And this time, we want the float type value 11.5 to int type value 12.


TO achieve this, we can use the methods provided by C#.


Converting a Float Data Type to an Int Data Type using method


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
	int x = 5;
	float y = 6.5F;
	int z = System.Convert.ToInt32(x + y);
	System.Console.WriteLine("The added value is : "+z);
    }
}	 


Output :



  The added value is : 12

And all we have done is, used System.Convert.ToInt32() method and passed the added value x+y to it as parameter.


int z = System.Convert.ToInt32(x + y);

And what happens is the float type data 11.5 gets converted to int type data 12.


Similarly, to convert int data type to string data type, we can use Convert.ToString() method.


To convert double to int we can use Convert.ToDouble() method. And so on.