C# List vs IEnumerable performance question

后端 未结 4 2028
伪装坚强ぢ
伪装坚强ぢ 2021-02-12 14:55

Hi suppose these 2 methods:

private List GetProviderForType(Type type)
        {
            List returnValue = new         


        
4条回答
  •  时光说笑
    2021-02-12 15:32

    An important part of this question is "how big is the data"? How many rows...

    For small amounts of data, list is fine - it will take negligible time to allocate a big enough list, and it won't resize many times (none, if you can tell it how big to be in advance).

    However, this doesn't scale to huge data volumes; it seems unlikely that your provider supports thousands of interfaces, so I wouldn't say it is necessary to go to this model - but it won't hurt hugely.

    Of course, you can use LINQ, too:

    return from provider in _objectProviders
           where provider.Key.IsAssignableFrom(type) ...
           select provider.Value;
    

    This is also the deferred yield approach under the covers...

提交回复
热议问题