C# Cannot convert from IEnumerable<Base> to IEnumerable

后端 未结 4 582
忘掉有多难
忘掉有多难 2020-12-19 07:53

I recently run into trouble when trying to AddRange(IEnumerable) to a List. Probably a classic issue, but I do not really get it yet.

I understand that methods expec

4条回答
  •  鱼传尺愫
    2020-12-19 08:17

    In C# 3.0 you can use the "Cast" extension method. If you import System.Linq and then use this code:

    public void AddRange2(IEnumerable foos) where T : Foo
    {
        m_Foos.AddRange (foos.Cast());
    }
    

    Then it should work for you.

提交回复
热议问题