给定此类:
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);
}
}
此代码段无法编译:
IEnumerable<int> value = new [] { 1, 2, 3 };
Wrapper<IEnumerable<int>> wrapper = value;
错误CS0266:无法将类型'System.Collections.Generic.IEnumerable <int>'隐式转换为'Spikes.Implicit.Wrapper <System.Collections.Generic.IEnumerable <int >>'。存在显式转换(您是否缺少演员表?)
但是编译器对此非常满意:
Wrapper<IEnumerable<int>> wrapper = new [] { 1, 2, 3 };
为什么?
www说
相关分类