Feedback

Generator Enumerable Extension Methode

Sprache: C#

Mit dieser Extension kann man über eine Generatorfunktion eine Liste mit diversen daten füllen. Benötigte usings sind [code] using System; using System.Collections.Generic; [/code]
public static class Extension
{

    public static void Generate<T>(this ICollection<T> coll, Func<T> generater, long CountOfItemsToGenerate)
    {
        if (generater == null)
            throw new ArgumentNullException("generater");

        if (coll == null)
            throw new NullReferenceException("The collection cannot be null.");

        ICollection<T> tempColl = coll;

        for (int i = 0; i < CountOfItemsToGenerate; i++)
        {
            tempColl.Add(generater());
        }
    }

}

//Benutzung
 class Program
    {
        static void Main(string[] args)
        {

            List<int> liste = new List<int>();
            liste.Generate(() => new Random().Next(1, 20), 10);
            
            Console.ReadKey();
            
        }
    }

public static class Extension
{

    public static void Generate<T>(this ICollection<T> coll, Func<T> generater, long CountOfItemsToGenerate)
    {
        if (generater == null)
            throw new ArgumentNullException("generater");

        if (coll == null)
            throw new NullReferenceException("The collection cannot be null.");

        ICollection<T> tempColl = coll;

        for (int i = 0; i < CountOfItemsToGenerate; i++)
        {
            tempColl.Add(generater());
        }
    }

}

//Benutzung
 class Program
    {
        static void Main(string[] args)
        {

            List<int> liste = new List<int>();
            liste.Generate(() => new Random().Next(1, 20), 10);
            
            Console.ReadKey();
            
        }
    }

2 Kommentare