1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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();
}
}
|