概念:
泛型就是通过参数类型来实现在同一份代码上操作多种数据类型
泛型编程是一种编程范式,它利用“参数化类型”将类型抽象画,从而实现更为灵活的复用
使用泛型时,参数化的类型可以设置多个,以逗号隔开。
泛型方法:
数据类型 名称<代表字符X>(代表字符X x):不一定用X
static void Main(string[] args)
{
Ff<int>(1); //Ff<数据类型>(赋值)
Ff<string>("qwe");
Ff<char>('a');
Console.ReadKey();
}
static void Ff<T>(T t) //泛型T T t
{
if (t.GetType()==typeof(int)) //如果T t中t的值是整数类型
{
Console.WriteLine("数字"); //那么输出“数字”
}
else if (t.GetType()==typeof(string))//如果T t中t的值是字符串类型
{
Console.WriteLine("字符串"); //那么输出“字符串”
}
else //否则
{
Console.WriteLine("未知"); //输出“未知”
}
}
-----------------------------------------------输出结果
数字
字符串
未知
泛型类:
class Program
{
static void Main(string[] args)
{
Le<int> le = new Le<int>(); //3.类名<对应的数据类型> 名称=new 类名();
le.FF(1);
Le<string> le1 = new Le<string>();
le1.FF("qwe");
Le<char> le2 = new Le<char>();
le2.FF('a');
Console.ReadKey();
}
}
class Le<T> //1.如果使用泛型类
{
public void FF(T t) //2.那么在方法上就不用写格式了 直接T t
{
if (t.GetType()==typeof(int))
{
Console.WriteLine("数字");
}
else if (t.GetType()==typeof(string))
{
Console.WriteLine("字符串");
}
else
{
Console.WriteLine("未知");
}
}
}
泛型接口:
interface Fanxingjiekou<T> //创建泛型接口
{
void Aa(T t);
}
class Class1:Fanxingjiekou<int> //创建类Class1 继承泛型接口
{
public void Aa(int a)
{
Console.WriteLine("数字"+a);
}
}
class Class2:Fanxingjiekou<string> //创建类Class2 继承泛型接口
{
public void Aa(string b)
{
Console.WriteLine("字符串"+b);
}
}
---------------------------------------------------
class Program //主方法输出
{
static void Main(string[] args)
{
Class1 C1 = new Class1(); //创建实例
C1.Aa(1); //()中的数据类型对应继承泛型接口的类的数据类型
Class2 C2 = new Class2();
C2.Aa("qwe");
Console.ReadKey();
}
}
泛型集合类+List<T>
泛型集合类
泛型集合类允许用户创建强类型集合,它能够提供比非泛型强类型集合更好的类型安全性和性能
泛型集合包含的类与非泛型包含的类集合一 一对应,是来取代非泛型集合对应的类的;
主要泛型集合类有:
List<T>
Dictionary<K,V>字典
Queue<T>
SortedList<T>
Stack<T>
List<T>
public int Count{get;}
获取syrtem.Collections.Genenric.List中实际包含的元素数
获取实际包含的元素个数
List<string> list = new List<string>() {"aa","bb","cc"};
Console.WriteLine(list.Count);
--->
3
public int Capacity{get;set;}
获取或设置该内部数据结构在不调整大小的情况下能够容纳的元素总数
获取可包含的元素个数
List<string> list=new List<string>(5){"aa","bb","cc"};
Console.WriteLine(list.Capacity);
---------------
5
List<T>类的一些常用的方法
public void Add(T item);
将对象添加到System.Collections.Generic.List'1的结尾处。
添加元素
List<string> list=new List<string>(){"aa","bb","cc"};
list.Add("dd")
foreach (var item in list)
{
Console.WriteLine(item);
}
------------------------------
aa
bb
cc
dd
Public void AddRange(IEnumerable<T>collection)
将指定集合的元素添加到System.Collections.Feneric.List'1的末尾
添加其他集合元素到此集合。
static void Main(string[] args)
{
List<string> list1 = new List<string>() { "qwe","asd","zxc"};
List<string> list2 = new List<string>() {"vbn","fgh","rty"};
list1.AddRange(list2);
foreach (var item in list1)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
public int BinarySearch(T item);
使用默认的比较器在整个已排序的System.Collections.Generic.List'1中搜索元素,并返回该元素从零开始的索引。
参数说明:
Item:要定位的对象。对于引用类型,该值可以为null
二分法查找元素。
static void Main(string[] args)
{
List<string> list1 = new List<string>() { "qwe","asd","zxc"};
List<string> list2 = new List<string>() {"vbn","fgh","rty"};
Console.WriteLine(list2.BinarySearch("fgh"));
Console.ReadKey();
}
public void Clear();
从System.Colleztions.Generic.List'1
移除所有元素。
static void Main(string[] args)
{
List<string> list1 = new List<string>() { "qwe","asd","zxc"};
List<string> list2 = new List<string>() {"vbn","fgh","rty"};
list2.Clear();
list2.Add("LLLLL");
foreach (var item in list2)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
public bool Contains(T item);
确定某元素是否包含在System.Collections.Generic.List'1中
参数说明:
item:要在System.Collections.Generic.List'1中定位的对象。对于引用类型,该值可以为null.
判断是否包含指定元素。包含返回true;否则false。
static void Main(string[] args)
{
List<string> list1 = new List<string>() { "qwe","asd","zxc"};
List<string> list2 = new List<string>() {"vbn","fgh","rty"};
Console.WriteLine(list2.Contains("fgh"));
Console.ReadKey();
}
public void CopyTo(int index,T[] array,int arraydex,int count);
从目标数组的指定索引出开始,将元素的范围从SYstemCollectionsGenericList'1复制到兼容的一维数组。
参数说明:
- index:即从源SystemCollectionsGenericList'1中从零开始的开始复制的索引。
- array:以为Syste,Array,它是从SystemCollectionsGenericList'1复制的元素的目标,SystemArray必须具有从零开始的索引。
- arrayIndex:array中从零开始的索引,从此处开始复制。
- Count:要将制定的元素复制到制定数组的指定位置。
将制定元素复制到制定数组的制定位置。
static void Main(string[] args)
{
List<string> list1 = new List<string>() { "qwe","asd","zxc"};
List<string> list2 = new List<string>() {"vbn","fgh","rty"};
string[] arr = new string[3];
list1.CopyTo(0,arr,0,3); //list1中下标0开始,被复制数组名,被复制到下标,被复制数量
foreach (var item in arr)
{
Console.WriteLine(item);
}
Console.ReadKey();
}