In today's tutorial, we are gonna have a look at Datatype Conversions in C#. We have discussed Datatypes in C# in our 3rd tutorial and today we will ...
Hello friends, I hope you all are doing great. In today's tutorial, we are gonna have a look at Datatype Conversions in C#. We have discussed C# Data Types in our 3rd tutorial and today we will discuss How to convert one datatype into another.
If you are working on some complex project, then you have to convert one form of data into another i.e. let's say you are getting bill value from barcode reader, this value must be a string and if you want to apply any arithmetic operation on it then you need to convert that string into integer or float. So, let's get started with Datatype Conversions in C#:
Datatype Conversions in C#
If you are working on some data driven application, then there's always a need to convert one form of data into another and that's where Datatype Conversions are required.
Few conversions doesn't require any method or class i.e. if we want to convert an integer into float then that can easily be done by the compiler, as there's no chance of data loss.
Such conversions are called Implicit Conversions, here's an example:
You can see in above figure that compiler has automatically converted integer into float.
But if we want to convert a float (let's say 313.56) into integer then we can't do that as integer doesn't have the decimal part.
So, if we want to convert a float into integer, we will get a compiler error i.e. overflow exception.
In order to perform such conversions we have to use Explicit Conversions method, pre-defined in C#.
There are two options for Explicit Conversions:
Use Cast Operator.
Use Convert class of C#.
Both of these methods are shown in below figure:
Now you can see in above figure that the Cast Operator (int) has just taken the main value and simply ignored the decimal/fractional part.
While the Convert Class (Convert.ToInt32) has rounded off the number and didn't ignore the decimal part.
String Conversion in C#
Normally the data received from external/hardware devices, is in the form of strings and we need to convert it into integer or float etc. to perform arithmetic operations.
There are two options available for String Conversions in c#, which are:
Parse.
TryParse.
Here's the implementation of Parse Method for String to integer conversion, in below figure:
But there's a drawback in Parse that if the string contains alphabetical / special characters then it will create an error and couldn't convert the string into integer.
So, in order to avoid error, we should use TryParse method, as shown in below figure:
As you can see in above figure, TryParse takes two parameters:
First one must be a string, which you want to convert.
Second one must be an integer, in which the value will be saved.
Moreover, TryParse method also returns a Boolean type, which will be:
True, if the conversion is successful.
False, if the conversion is unsuccessful.
I have provided a string "323ABC" whcih contains alphabetical characters, that's why ConversionCheck is false and we are not getting any value in our integer.
Here's a screenshot of one successful conversion of TryParse method:
Now TryParse is returning True as the conversion is successful and our output integer variable got updated.
Here's the code used in today's tutorial:
using System;
namespace TEPProject
{
class Program
{
static void Main(string[] args)
{
Console.Write("\n\nwww.TheEngineeringProjects.com\n");
string Num1 = "323";
bool ConversionCheck = int.TryParse(Num1, out int Num2);
Console.WriteLine("\nValue of Num2 : {0}", Num2);
Console.WriteLine("\nValue of ConversionCheck : {0}", ConversionCheck);
if(!ConversionCheck)
{
Console.WriteLine("\nString contains non-numeric Values.");
}
else
{
Console.WriteLine("\nOur Converted int : {0}", Num2);
}
}
}
}
So, that was all about the Datatype Conversions in C#. I hope you have enjoyed today's tutorial. In the next tutorial, we will have a look at How to use Arrays in C#. Till then take care & have fun !!! :)
syedzainnasir
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a
programmer since 2009 before that I just search things, make small projects and now I am sharing my
knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+Follow
Get Connected
Comments on ‘’ Datatype Conversions in C# ‘’ ( 0 )