microsoft / dotnet

This repo is the official home of .NET on GitHub. It's a great starting point to find many .NET OSS projects from Microsoft and the community, including many that are part of the .NET Foundation.
https://devblogs.microsoft.com/dotnet/
MIT License
14.34k stars 2.21k forks source link

TrueForAll is missing in IList<T>. It is present only in implementation - List<T> #1301

Closed flamaniac closed 2 years ago

flamaniac commented 3 years ago

I prefer to always use interface IList when passing array collection but TrueForAll method is missing for this interface.

jcurl commented 3 years ago

I regularly implement my own collections against an IList, that would mean I would have to add TrueForAll even if I don't need it. Have you considered an extension method for your code?

flamaniac commented 3 years ago
  1. Yes, but not why making this as a part of the framework?

  2. BTW I have thought a little about extension method TrueForAll or any other predicate implementation based on interface (IList) and based on implemented type (List)

For extension method you cannot optimize iteration count. For example for:

IList<T> listInterface;
listInterface.Where(...).Where(...).TrueForAll()

will make 3 iterations... each iteration for each predicate.

For List you can optimize this just by remembering predicate that you need to apply on each item and just apply those 3 predicates in the single loop.

It seems that better is to add this method to IList and add optimized implementation for each class that implements this interface. But yes. I understand that interface change is breaking change for the framework and only possibility is to add another interface (child of IList) or create extension method witch will have not nice computational complexity. Hymm...