IList Extension Methods

The following extension methods are defined for the IList<T> type:

  • bool IsNullOrEmpty().
    Determines whether the IList<T> specified is null or has no elements (true) or has elements (false).
  • void SwapValues<T>(int indexA, int indexB).
    Swaps the values at indexA and indexB in the IList<T> specified.
  • void AddRange<T>(IEnumerable<T> toAdd).
    Adds the range toAdd to the IList<T> specified. No-op if toAdd is null or the IList<T> specified is null
  • int BinarySearch<T>(T element, IComparer<T> comparer).
    Searches for the element specified in the sorted list specified using binary search http://en.wikipedia.org/wiki/Binary_search. The algorithm is re-implemented here to be able to search in any sorted IList implementing data structure (.NET's BCL only implements BinarySearch on arrays and List<T>. Returns the index of the element searched or the bitwise complement of the index of the next element that is larger than element or if there is no larger element the bitwise complement of Count. Bitwise complements have their original bits negated. Use the '~' operator in C# to get the real value. Bitwise complements are used to avoid returning a value which is in the range of valid indices so callers can't check whether the value returned is an index or if the element wasn't found. If the value returned is negative, the bitwise complementcan be used as index to insert the element in the sorted list to keep the list sorted. Assumes that the IList<T> specified is sorted ascending. If you pass in a descending sorted list, be sure the comparer is adjusted as well.

Last edited Nov 27, 2010 at 1:41 PM by SolutionsDesign, version 3

Comments

PauloMorgado Dec 5, 2010 at 5:33 PM 
An extension method should behave like an instance method. IsNullOrEmpty should not be an extension method.