Move element To First in List - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:List

Description

Move element To First in List

Demo Code


using System.Linq;
using System.Text;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;

public class Main{
        public static List<TSource> MoveToFirst<TSource>(this List<TSource> source, TSource element)
        {/*from  www  .ja v a2s .co m*/
            if (!source.Contains(element))
                return source;

            source.Remove(element);
            source.Insert(0, element);
            return source;
        }
        public static bool Contains<T>(this IEnumerable<T> me, Predicate<T> condition)
        {
            foreach (var val in me)
            {
                if (condition(val)) 
                    return true;
            }
            return false;
        }
}

Related Tutorials