Variable Declaration Implicit Vs Explicit

Sometimes we have to change the data that we are using. Sometimes this change may change the value of the data or sometimes it just changes the memory space allocated by that data. This change is called data type conversion.

Data type is converted in 2 ways. They are:

  1. Implicit conversion
  2. Explicit conversion

They are described below.

Implicit conversion:

An implicit conversion converts data automatically without losing any data. Normally, data types can implicitly converted into the data types those contain more memory space.

Example:

short firstNumber = 65;

long secondNumber = firstNumber;

Explicit conversion:

An explicit conversion convert data that are automatically can’t be converted. It may converts data from bigger size to smaller size or converts data from one type into totally different type.

Example:

short firstNumber = 65;

long secondNumber = firstNumber; //Implicit conversion

short thirdNumber = (short)secondNumber; //Explicit  conversion

Console.WriteLine(thirdNumber); //outputs 65

char a = (char)firstNumber; //Explicit conversion

Console.WriteLine(a); //outputs A

We can use some built in functions those are provided with C#.

a. Convert class:

A convert class contains numerous method those can change one data type into another type. It is another implementation of explicit conversion.

Example:

int number = 65;

char character = Convert.ToChar(number);    //Conversion using System’s Class

Console.WriteLine(character);   //Output: A

b. ToString() method:

A ToString() method converts any kind of data type into String type. It doesn’t change the internal value of that data.

Example:

char character = Convert.ToChar(65);    //Conversion using System’s Class

string numberString = (65).ToString();

Console.WriteLine(character);   //Output: A

Console.WriteLine(numberString);    //Output(value is in String form): 65

Advertisements

About Asif
Hello Friends… I have done my Bachelor of Engineering in Information Technology from Gujarat University. I have 1.7 years experience of web and windows based application development.. My basic idea to start a blog is to store all important link which help me in my application and access to every where. Also this information is help others who want to makes their application powerful. I am interested in .net framework (1.0,2.0,3.0,3.5 and more). This blog contain also many information related to SQL, Custom Controls in C#.net Please share your ideas and any query if you have...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: