1

I've got trouble with type conversion with following code:

public class pr<T>
{
    private T tt;

    public pr( T value ) {
        this.tt = value;
    }

    public static explicit operator T(pr<T> op)
    {
        return default(T);
    }

    public static explicit operator pr<T> (T op)
    {
        return null;
    } 
}

Usage:

        byte value = 255;
        pr<byte> property = new pr<byte>(50);

        property = (pr<byte>)value; // no error here, works well test it throught debugger.
        value = (pr<byte>)property; // An explicit conversion exists are u missing cast?

Please tell me what i'm doing wrong. I'm just a begginer and rly don't understand what i supposed to do. I apologize for my bad english. Thank you. P.S. implicit conversion works fine.

2 Answers2

2
value = (byte)property;

Modify second line as above. Your target type in byte not pr<byte>

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
0

Change

value = (pr<byte>)property; // An explicit conversion exists are u missing cast?

into

value = (byte)property; // An explicit conversion exists are u missing cast?
Amir Shrestha
  • 451
  • 5
  • 11