Implicit keyword :
The implicit keyword is used to declare an implicit user-defined type conversion operator.
EXAMPLE:
struct Digit
{
byte value;
public Digit(byte value)
{
this.value = value;
}
public static implicit operator byte(Digit d)
{
Console.WriteLine( "implicit conversion occurred" );
return d.value;
}
}
Class ImExUsage
{
public static void Main()
{
Digit d1 = new Digit(3);
// implicit (no cast) conversion from Digit to byte
byte b1 = d1;
}
}
Explicit keyword :
The explicit keyword is used to declare an explicit user-defined type conversion operator.
EXAMPLE:
struct Digit
{
byte value;
public Digit(byte value)
{
this.value = value;
}
public static explicit operator Digit(byte b)
{
Digit d = new Digit(b);
Console.WriteLine("explicit conversion occurred");
return d;
}
}
Class ImExUsage
{
public static void Main()
{
byte b2 = 3;
//explicit conversion
Digit d2 = (Digit)b2;
}
}
Download Source
Regards,
Satheesh



0 Comments:
Post a Comment
<< Home