Data Type Conversion In C#



Data types:


When we instruct a machine to execute some operation, the machine changes the high
level language (which we are sending) into machine code (sequence of 1s and 0s).
The instruction we are sending also contains data on which the operation will be
executed.
But not all data should contain the same amount of memory nor do the same types of
operation. So, different sets of data have different size and perform different types of
operation. And they are not interpreted in the same way.
So, programmer needs to tell the system before-hand, the type of numbers or characters
he is using in his instruction (coding). These types are called Data Types. There are many
data types in C# language.
There are two categories of Data Type.

i. Predefined (Primitive) Data Types
ii. User defined Data Types

Predefined Data Types:

Different types of predefined data types allocated different amount of memory
and performs different types of operation.
The following data table lists the predefined data types and describes the data
that they are designed to store.

Type Description Range Suffix
byte 8-bit unsigned integer 0 to 255
sbyte 8-bit signed integer -128 to 127
short 16-bit signed integer -32,768 to 32,767
ushort 16-bit unsigned integer 0 to 65,535
int 32-bit signed integer -2,147,483,648
to
2,147,483,647
uint 32-bit unsigned integer 0 to 4,294,967,295 u
long 64-bit signed integer -9,223,372,036,854,775,808
to
9,223,372,036,854,775,807
l
ulong 64-bit unsigned integer 0 to 18,446,744,073,709,551,615 ul
float 32-bit Single-precision floating point type -3.402823e38 to 3.402823e38 f
double 64-bit double-precision floating point type -1.79769313486232e308 to 1.79769313486232e308 d
decimal 128-bit decimal type for financial and monetary calculations (+ or -)1.0 x 10e-28
to
7.9 x 10e28
m
char 16-bit single Unicode character Any valid character, e.g. a,*, \x0058 (hex), or\u0058 (Unicode)
bool 8-bit logical true/false value True or False
object Base type of all other types.
string A sequence of Unicode characters
DateTime Represents date and time 0:00:00am 1/1/01
to
11:59:59pm 12/31/9999

User defined data types:

User defined data types are composed of different combination of predefined
data types. Sometimes a user defined data type contains another user defined data
type in it. A user defined data type can also perform some operations and the
definition of the operation will be stated into the type definition.
Working with data types:
To use a data in our program, we have to follow some specified way and use
some specific keywords that are defined by the language creators.
int numberOfItems = 100;
double priceWithVAT = 100.50;
In the above two lines, I have declared two variables and initialized these with
values. Even you can declare a variable without initializing it.
int numberOfItems;
double priceWithVAT;

But the following two lines will not compiled because I am trying to use
numberOfItems as a parameter of WriteLine method without initializing it.
It’s not possible to use a variable without initializing it.
int numberOfItems;
Console.WriteLine(numberOfItems);
Now try this:
decimal priceWithVAT = 100.50;
You will get an error. Compiler can’t convert a double value to decimal type.
decimal priceWithVAT = 100.50M;
or
decimal priceWithVAT = Convert.ToDecimal(100.50);
or even
decimal priceWithVAT = (decimal)100.50;

Data Type: String

As stated above, a string variable contains a sequence of alphanumeric
characters. We can declare a string variable like other types of variable.
Example:
string name;
string firstName = "Foyzul";
Escape characters:
Escape characters are used when we want to set a run time meaning of a
part of a string.
Example:
string greetings = "Hello everyone.\nWelcome to OOP Class.";
Verbatim characters:
A verbatim string is a string that is interpreted by the compiler exactly as
it is written, which means that even if the string spans multiple lines or
includes escape characters, these are not interpreted by the compiler and
they are included with the output. The only exception is the quotation
mark character, which must be escaped so that the compiler can
recognize where the string ends.
Example:
string address = @"C:\Software\Books\Beginning”;
As you can see, verbatim is indicated with an ‘@’ sign at the beginning
of a string.
If you want to use a quotation mark inside a verbatim string, you must
escape it by using another set of quotation marks.
Example:
string greetings = @"""Hello everyone"".said the trainer.";

Data Type: Constants

A constant is a variable whose value remains constant. Constants are useful in
situations where the value that you are using has meaning and is a fixed number,
such as pi, the radius of the earth, or a tax rate.
Example:
const int radiusOfEarth = 6378;
As you can see, constant variable starts with the keyword ‘const’ at the
declaration, or initialization.

Data Type Conversions:

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:

a. Implicit conversion
b. 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

Post a Comment

Previous Post Next Post