sharpnewbie
sharpnewbie

Reputation: 21

cast missed c# explicit conversion

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.

Upvotes: 1

Views: 107

Answers (2)

Amir Shrestha
Amir Shrestha

Reputation: 451

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?

Upvotes: 0

Akshey Bhat
Akshey Bhat

Reputation: 8545

value = (byte)property;

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

Upvotes: 2

Related Questions