Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 467
Jose Avatar asked May 07 '09 13:05

Jose


1 Answers

public static implicit operator T(SelectItem<T> obj) {
    return obj.Item;
}
like image 62
mmx Avatar answered Sep 28 '22 10:09

mmx