Friday, 21 December 2012 13:54

data type conversion in csharp

Written by 
Rate this item
(0 votes)

One great job programmers do is to convert one type to
another type.there are 2 type of conversions.

one is explicit conversion and the other is implicit conversion.
implicit conversion is done by the language for you.
example is -
int integer=12;
single single=integer;

but the explicit conversion is done by programmers in the
programming code as a example for this follows-
single sin=123.45;
int inte=(int)single;

conversion is widely used where it is needed to get
numbers(in string format) from database or form to
converted number format for use in the program for calcu-
lations or comparison purposes.to convert from string
to something else,built in conversion function of the
string class is available and to convert from any
type to string,Tostring() function is used.
as a example-
let us declare a string variable -
string st1="123";
to convert it to single,Tosingle() command is used as
follows-single sing=st1.Tosingle();

one thing need to keep in mind is that you do not convert
the string(string st1=1234.56 , a big floating point number
in string format) from it's normal format to an integer
format directly,but first convert it to floating point
(single) number and then you will cast it to integer.
question may come why actually have to do this?

the Toint16() function have problem with nonvalid
formats,which it considers a flat(1234.56) to be.so
it throw exceptions of invalid format.to avoid that
you have to do the following to convert from string to
integer-
string s1="1234.56";
int in1=(int)s1.ToSingle();

hope this will help programmers to keep in mind in case
of that kind of conversion.

Read 4552 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.