Given this class:
public class Wrapper<T>
{
public Wrapper(T value)
{
Value = value;
}
public T Value { get; }
public static implicit operator Wrapper<T>(T value)
{
return new Wrapper<T>(value);
}
}
This code snippet doesn't compile:
IEnumerable<int> value = new [] { 1, 2, 3 };
Wrapper<IEnumerable<int>> wrapper = value;
error CS0266: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable< int>' to 'Spikes.Implicit.Wrapper< System.Collections.Generic.IEnumerable< int>>'. An explicit conversion exists (are you missing a cast?)
But the compiler is perfectly happy with this:
Wrapper<IEnumerable<int>> wrapper = new [] { 1, 2, 3 };
Why?