Type casting :
1) cast double data type into int :
int ans ;
double a=10.5;
double b =20.3;
ans = (int)( a / b) ; //cast from double to int .
2) cast int data type into byte :
if int value is less thn 255 then there is no data lost .
but if integer value is greater than 255 there will be data lost .
byte b ;
int i=257 ;
b =(byte) i ; // cast from int to byte .
Console.WriteLine(b);
3) Cast uint data type into short :
uint uval ;
short sval ;
uval =23000;
sval =(short) uval ; // cast from Uint to short with no data lost
Console.WriteLine(s);
if uval is greater than 32767 then data will be lost .
4) cast long into uint :
uint uval ;
long lval ;
long =64000;
uval =(uint) lval ; //cast from long to uint with no data lost .
if long value is negative then there will be data loss .
5) cast byte into char
byte bval ;
char ch ;
bval =88;
ch =(char)bval ; // output x without data loss.
1) cast double data type into int :
int ans ;
double a=10.5;
double b =20.3;
ans = (int)( a / b) ; //cast from double to int .
2) cast int data type into byte :
byte b ;
int i=100;
b =(byte) i ; // cast from int to byte .
Console.WriteLine(b);
if int value is less thn 255 then there is no data lost .
but if integer value is greater than 255 there will be data lost .
byte b ;
int i=257 ;
b =(byte) i ; // cast from int to byte .
Console.WriteLine(b);
3) Cast uint data type into short :
uint uval ;
short sval ;
uval =23000;
sval =(short) uval ; // cast from Uint to short with no data lost
Console.WriteLine(s);
if uval is greater than 32767 then data will be lost .
4) cast long into uint :
uint uval ;
long lval ;
long =64000;
uval =(uint) lval ; //cast from long to uint with no data lost .
if long value is negative then there will be data loss .
5) cast byte into char
byte bval ;
char ch ;
bval =88;
ch =(char)bval ; // output x without data loss.
No comments:
Post a Comment