泛型
public static void Swap<T>(ref T value0, ref T value1)
{
T temp = value0;
value0 = value1;
value1 = temp;
}
为了防止传入的类型导致方法运行出错,可以对泛型进行一定的约束
约束关键词:where
例如:
public class Person { }
class Program
{
public static void S<T>( T t)where T:class
{
}
static void Main(string[] args)
{
S(new Person());
}
}
如果有多个约束条件,可以自行尝试
集合的概念
集合可分为两类:泛型集合和非泛型集合
非泛型集合
泛型集合
例如:
ArrayList array = new ArrayList();
array.Add(1);
array.Add(2);
array[0] = 3;
array.Insert(0, 4);
array.Remove(2);
array.RemoveAt(0);
array.Reverse();
bool res = array.Contains(3);
array.Clear();
int length = array.Count;
int index = array.IndexOf(3);
List<int> list = new List<int>();
list.Add(3);