| Casting and Class conversion | also see implicit data conversion |
| implicit means upcasting , e.g, int is casted to
long data types. explicit means downcast, where larger type is casted into smaller data type. |
|
using System;
//csc implicit_explicit_3.cs
namespace try_imex
{
public class im_ex
{
public im_ex() { Console.WriteLine("Constructor Evoked");}
~im_ex() { Console.WriteLine("Destructor Evoked");}
public double test_implicit(int xn, double xd)
{
//implicit conversion lower to higher with no data loss
int i = xn;
double d = xd;
// An implicit conversion, no data will be lost.
Console.WriteLine("what is i := " + i + " And What is d := " + d);
// converting explicitly
i = (int)d;
Console.WriteLine("what is i again:= " + i + " And What is d again: " + d);
return(i + d);
}
//class im_ex ends
}
class test
{
public static void Main()
{
im_ex imex = new im_ex();
Console.WriteLine("Enter an Intger");
string str = Console.ReadLine();
int nn = Int32.Parse(str);
Console.WriteLine("Enter an Double");
str = Console.ReadLine();
double dd = double.Parse(str);
//calling the method
imex.test_implicit(nn, dd);
}
}
//namespace try_imex ends
}
|
|
![]() |
|
| Note the data lost due to casting; the code will not compile without
casting double data type into integer data type.
|
|
![]() |
|