ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

Kerollos Adel

insert item in the top of list

Source

public static List<T> InsertFirst<T>(this  List<T> lst, T Obj)
{
    List<T> NewList = new List<T>();

    NewList.Add(Obj);
    NewList.AddRange(lst);

    return NewList;
}

Example

List<int> NumberList = new List<int>() {0 , 1, 2, 3, 4, 5, 6, 7 };
NumberList = NumberList.InsertFirst(-1);

Author: Kerollos Adel

Submitted on: 16 jun. 2014

Language: C#

Type: Insert

Views: 4150