|
|
Generische Methode
A06.cs
using System;
using System.Collections.Generic;
class Test {
// copies an array of T to a list of T
static List< T > Copy< T >(T[] arr) {
List< T > list = new List< T >();
foreach (T x in arr) list.Add(x);
return list;
}
public static void Main() {
//--- try it on an int array
int[] intArr = {3, 2, 6, 4, 5};
List<int> intList = Copy(intArr);
foreach (int x in intList) Console.Write(x + " ");
Console.WriteLine();
//--- try it on a string array
string[] stringArr = {"to", "be", "or", "not", "to", "be"};
List<string> stringList = Copy(stringArr);
foreach (string x in stringList) Console.Write(x + " ");
Console.WriteLine();
}
}
|
|