List AddElement

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"});

Description

Make adding to list fluent and conditioned

Details

Double click on the code to select all.

 

;