Extension method for List<T> AddToFront(T object) how to?

Viewed 54788

I want to write an extension method for the List class that takes an object and adds it to the front instead of the back. Extension methods really confuse me. Can someone help me out with this?

myList.AddToFront(T object);
2 Answers

Instead of using List, you can use IList interface which provides the extension methods for many more collections like BindingList or more. Here are my extension methods for list

Syntax:

public static class ExtensionMethods
{
    public static void AddToFront<T>(this IList<T> list, T item)
    {
         if(list is null) throw new ArgumentNullException("Can't add items to null List");
         list.Insert(0, item);
    }

    public static bool IsNullOrEmpty<T>(this IList<T> list)
    {
         if(list is null || list.Count == 0) return true;
         return false;
    }
}
Related