您的位置:首页 > 编程语言 > C#

c#入门第十五课

2020-07-28 12:27 1226 查看

泛型

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: