Header Ads

C# Tutorial - 5. data type conversion in C#

data type conversion in C#


Welcome to the fifth lesson of my Visual C# Tutorial. In this lesson, we are going to learn how to convert data type of variables. As far as C# programming is concerned, data conversion is basically taking something from one data type of variable and converting into another data type without changing the actual values.

At some point of writing an application in Visual C#, you are going to need to convert numeric value into a string value and sometimes vice versa. However, as you may guess, we can't simply treat a string variable as an integer variable or integer variable as a string variable. This is because those two variables are different in size based on the actual data that they will hold. That is why we need to learn how to convert data type of variables.

Firstly, let's add a bunch of codes for an example of data type conversion into the project that we used in the previous lesson. Please make sure to have the same codes as following.

data type conversion in C#


Step 1. Implicit data conversion                                                                 

Let's take a close look at first two lines of code that we wrote. Basically, we declared int variable called x with the numeric value of 10 assigned and string variable called y with the textual value of "Michael" assigned. Then, another string variable called conversion where the two variables are put together, and I am passing the values of conversion variable into WriteLine() method through parameter. 

What do you expect to be displayed on the console window? Is it possible for this application to treat the integer value of 10 as if it were a string? The answer is Yes. Let's get it to run and see what we are going to get. 


data type conversion in C#





As you can see, the value of 10 from variable x is displayed with "Michael" which is the value of variable y. This is because the .net is able to treat an integer value as an alphanumeric value which can be dealt by string variable. It is called Implicit Conversion. You don't actually need to do anything special for this kind of conversion. Everything is taken care by the .net. 

However, implicit conversion is not a good way to convert one data type into another because you might end up getting less understanding of how your application work. So let's convert the integer value into a string value by explicit conversion.

Step 2. Explicit Conversion                                                                       

Most of the data types in .net framework have add-on methods for performing certain type of tasks, and some of them are for conversion. Let's add the following code to perform explicit conversion from an integer into a string.

data type conversion in C#
As you can see, I added ".ToString" which takes the integer value and converts into a string value. Then you will get the same result.

        Integer variable + . + ToString() 
Then, one thing that I want you to notice is that the "+" operator in conversion variable is actually catenating two different alphanumeric values together. It means that this "+" operator would performing either catenating or adding mathematically based on the context.
 
Step 3. Conversion from a string to an integer                                             

What if we want to use a string variable in an integer variable? Since C# is a typed language which means that data types must be declared. So we should perform explicit conversion from a string to an integer variable. Let's add following code.

As you can see, I added a line of code "int.Parse()". It basically takes any variable through its parameter and makes it integer variable. That is why I put the variable y in the parameter. As i have mentioned above, data types in .net have methods for conversions and they can be called.

data type conversion in C#



Actually, there are many other types of conversions as far as C# programming is concerned, and we are going to cover them throughout this series of tutorial.


No comments

Powered by Blogger.