List AddElement
Make adding to list fluent and conditioned
Source
public static class ListExtensions
{
public static List<T> AddElement<T>(this List<T> list, T item)
{
list.Add(item);
return list;
}
public static List<T> AddElementIf<T>(this List<T> list, bool condition, T item)
{
if (condition)
{
list.Add(item);
}
return list;
}
public static List<T> AddElementRange<T>(this List<T> list, IEnumerable<T> items)
{
list.AddRange(items);
return list;
}
public static List<T> AddElementRangeIf<T>(this List<T> list, bool condition, IEnumerable<T> items)
{
if (condition)
{
list.AddRange(items);
}
return list;
}
}
Example
var list = new List<string>();
var condition = true;
list.AddElement("line 1")
.AddElementIf(condition, "line 2")
.AddElementRange(new[] {"line 3", "line 4"})
.AddElementRangeIf(condition, new[] {"line 5", "line 6"});
Author: Lasse Sjørup
Submitted on: 11 apr. 2013
Language: C#
Type: System.Collections.Generic.List<T>
Views: 6530