Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

151 lines (131 sloc) 5.074 kB
using System.Diagnostics;
using System.Linq;
namespace System.Collections.Generic
{
public static class GenericEnumerableExtensions
{
public static void Fill<T>(this IList<T> list, T value)
{
if (list.Contains(value)) return;
list.Add(value);
}
public static void Fill<T>(this IList<T> list, IEnumerable<T> values)
{
list.AddRange(values.Where(v => !list.Contains(v)));
}
/// <summary>
/// Removes all of the items that match the provided condition
/// </summary>
/// <typeparam name="T">The type of the items in the list</typeparam>
/// <param name="list">The list to modify</param>
/// <param name="whereEvaluator">The test to determine if an item should be removed</param>
public static void RemoveAll<T>(this IList<T> list, Func<T, bool> whereEvaluator)
{
for (int i = list.Count - 1; i >= 0; i--)
{
if (whereEvaluator(list[i])) list.RemoveAt(i);
}
}
/// <summary>
/// Concatenates a string between each item in a list of strings
/// </summary>
/// <param name="values">The array of strings to join</param>
/// <param name="separator">The value to concatenate between items</param>
/// <returns></returns>
public static string Join(this string[] values, string separator)
{
return String.Join(separator, values);
}
/// <summary>
/// Concatenates a string between each item in a sequence of strings
/// </summary>
/// <param name="values"></param>
/// <param name="separator"></param>
/// <returns></returns>
public static string Join(this IEnumerable<string> values, string separator)
{
return Join(values.ToArray(), separator);
}
/// <summary>
/// Performs an action with a counter for each item in a sequence and provides
/// </summary>
/// <typeparam name="T">The type of the items in the sequence</typeparam>
/// <param name="values">The sequence to iterate</param>
/// <param name="eachAction">The action to performa on each item</param>
/// <returns></returns>
public static IEnumerable<T> Each<T>(this IEnumerable<T> values, Action<T, int> eachAction)
{
int index = 0;
foreach (T item in values)
{
eachAction(item, index++);
}
return values;
}
[DebuggerStepThrough]
public static IEnumerable<T> Each<T>(this IEnumerable<T> values, Action<T> eachAction)
{
foreach (T item in values)
{
eachAction(item);
}
return values;
}
[DebuggerStepThrough]
public static IEnumerable Each(this IEnumerable values, Action<object> eachAction)
{
foreach (object item in values)
{
eachAction(item);
}
return values;
}
public static TReturn FirstValue<TItem, TReturn>(this IEnumerable<TItem> enumerable, Func<TItem, TReturn> func)
where TReturn : class
{
foreach (TItem item in enumerable)
{
TReturn @object = func(item);
if (@object != null) return @object;
}
return null;
}
public static IList<T> AddMany<T>(this IList<T> list, params T[] items)
{
return list.AddRange(items);
}
/// <summary>
/// Appends a sequence of items to an existing list
/// </summary>
/// <typeparam name="T">The type of the items in the list</typeparam>
/// <param name="list">The list to modify</param>
/// <param name="items">The sequence of items to add to the list</param>
/// <returns></returns>
public static IList<T> AddRange<T>(this IList<T> list, IEnumerable<T> items)
{
items.Each(list.Add);
return list;
}
public static bool IsEqualTo<T>(this IEnumerable<T> actual, IEnumerable<T> expected)
{
var actualList = actual.ToArray();
var expectedList = expected.ToArray();
if (actualList.Length != expectedList.Length)
{
return false;
}
for (int i = 0; i < actualList.Length; ++i)
{
if (!actualList[i].Equals(expectedList[i]))
{
return false;
}
}
return true;
}
public static IEnumerable<T> UnionWith<T>(this IEnumerable<T> first, params T[] second)
{
return first.Union(second);
}
}
}
Jump to Line
Something went wrong with that request. Please try again.