|
C# : Data conversion : Explicit & Implicit Data Conversion |
| Implicit Data conversion example (also read keyword implicit
and explicit) |
Explicit
Conversion |
| The data type conversion is an important aspect of any object oriented
programming. |
| |
using System;
//csc implicit_explicit.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)
{
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);
// since it is a value type data it will not change i in the above line
// the line below will throw an error about
i = 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");
int nn = Int32.Parse(Console.ReadLine());
Console.WriteLine("Enter an Double");
double dd = Int64.Parse(Console.ReadLine());
//calling the method
}
}
//namespace try_imex ends
}
|
|
 |
| |

|
Now remark // i =d ;
using System;
//csc implicit_explicit_1.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)
{
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);
// since it is a value type data it will not change i in the above line
// the line below will throw an error about
//i = 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
}
|

|
Now convert int data to double implicitly
using System;
//csc implicit_explicit_2.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)
{
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);
// since it is a value type data it will not change i in the above line
// the line below will throw an error about
//i = d;
//reverse will implicit (automatically) convert
d = i;
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
}
|

|
| |