Transform ICollection to List - CSharp System.Collections

CSharp examples for System.Collections:ICollection

Description

Transform ICollection to List

Demo Code


using System.Collections.Generic;
using System.Collections;
using System;/* ww w.  j  a  va  2 s .  co  m*/

public class Main{
        public static List<T> Transform<F, T>(ICollection<F> source)
            where F : T
        {

            if (source == null || source.Count == 0)
                return new List<T>();

            List<T> ret = new List<T>(source.Count);
            foreach (F f in source)
            {
                ret.Add(f);
            }

            return ret;
        }
        public static List<T> Transform<F, T>(ICollection<F> source, Transformer<F, T> transformer)
        {
            if (transformer == null)
                throw new ArgumentException("transformer");

            if (source == null || source.Count == 0)
                return new List<T>();

            List<T> ret = new List<T>(source.Count);
            foreach (F f in source)
            {
                ret.Add(transformer(f));
            }

            return ret;
        }
}

Related Tutorials