简体   繁体   中英

Implicit operator?

I need some help. I am creating a SelectItem class like this:

public class SelectItem<T> where T : class
{
    public bool IsChecked { get; set; }
    public T Item { get; set; }
}

I would like the following code to be valid

SelectItem<String> obj = new SelectItem<String> { Item = "Value" };

obj.IsChecked = true;

String objValue = obj;

Instead of having to do this:

String objValue = obj.Item;

How can I accomplish this?

public static implicit operator T(SelectItem<T> obj) {
    return obj.Item;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM