給定此類: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;錯(cuò)誤CS0266:無法將類型'System.Collections.Generic.IEnumerable <int>'隱式轉(zhuǎn)換為'Spikes.Implicit.Wrapper <System.Collections.Generic.IEnumerable <int >>'。存在顯式轉(zhuǎn)換(您是否缺少演員表?)但是編譯器對此非常滿意:Wrapper<IEnumerable<int>> wrapper = new [] { 1, 2, 3 };為什么?
無法隱式“包裝” IEnumerable
人到中年有點(diǎn)甜
2021-04-02 21:17:11