Class with List<T> functions, only without Add, Remove and the like
Which C# class provides all List<T> functionality (especially Find, FindAll - with predicates, foreach, Count) but does not provide the Add, Remove functionality (and the like)?
ReadOnlyCollection<T>, IList<T>, ICollect开发者_Go百科ion<T>, IEnumerable<T> dont'have the Find's.
You can easily implement Find for IEnumerable:
public static T Find<T>(this IEnumerable<T> collection, T element)
{
return collection.First(x => x == element);
}
Edit:
If you don't want an exception when nothing is found, use FirstOrDefault instead of First.
ReadOnlyCollection<T> provides most of them. You can add the finds with extensionmethods or by deriving your own class from ReadonlyCollection<T>.
But Where does almost the same thing as FindAll, you just need a ToList() if you need a list instead of a lazy enumerable.
And Find does the same thing as FirstOrDefault(pred)
You could quite easily build your own class backed by a List<T> and only implement the functions you want. This would also give you the ability to add/remove data when you do need to through the use of internal methods.
加载中,请稍侯......
精彩评论