一、泛型集合概述
我们定义一个类、一个方法、一个集合的时候,并不明确的规定他是什么类型,但是当使用的时候,一开始必须要明确类型,而且明确后就不能修改。
二、泛型集合List<T>的使用
List<T> :泛型集合定义规范,<T>表示泛型,T是Type的简写,表示类型T可以换成其他的如T1,T2,S,M等等;
我们根据使用需要,把T换成具体的数据类型, 如:
List<T>结构:
0 <T>
1 <T>
2 <T>
3<T>
4<T>
...
前面是索引,后面是制定的数据类型,和数组差不多。泛型里面的T是我们使用的所有类型,
可以是基本类型int,char等,也可以是对象类型。
using System.Collections.Generic;
namespace ConsoleApp25
{
internal class Program
{
static void Main(string[] args)
{
List<string> Name = new List<string>();
Name.Add("name1");
Name.Add("name2");
Name.Add("name3");
//Name.Add(10);错误的,因为已经定义成string
}
}
}
//添加元素用.Add方法即可
三、常见的泛型遍历方法,foreach和for 的用法
using System;
using System.Collections.Generic;
namespace ConsoleApp25
{
internal class Program
{
static void Main(string[] args)
{
Test1();
Console.ReadLine();
}
static void Test1()
{
List<string> Name = new List<string>();
Name.Add("name1");
Name.Add("name2");
Name.Add("name3");
//Name.Add(10);错误的,因为已经定义成string
Test2(Name);
}
static void Test2(List<string> name)
{
foreach (string item in name)//foreach循环
{
Console.WriteLine(item);
}
Console.WriteLine("----------------");
for (int i = 0; i < name.Count; i++)/for/循环
{
Console.WriteLine(name[i]);
}
}
}
}
四、泛型集合的常用方法
static void Test3()
{
//集合初始化器
List<string> Name=new List<string>()
{
"名字1","名字2","名字3","名字4"
};
foreach (string item in Name)
{
Console.Write(item);
}
Console.WriteLine("以上是遍历Name集合内容的");
//复制集合
List<string> NewName = new List<string>(Name);
foreach (string item in NewName)
{ Console.Write(item); }
Console.WriteLine("以上是复制集合内容NewName");
//获取集合长度
int Lengh=Name.Count;
Console.WriteLine(#34;集合长度={Lengh}");
Console.WriteLine("+++++++++++");
//集合转换成数组
string[] NameArray= Name.ToArray();
for (int i = 0; i < NameArray.Length; i++)
{
Console.WriteLine(#34;这个是集合数组的内容={NameArray[i]}");
}
//将数组添加到集合中
string[] NewNameArray = { "名字5","名字6"};
Name.AddRange(NewNameArray);
for (int i = 0; i < Name.Count; i++)
{
Console.WriteLine(#34;这个是在集合末尾加数组的集合={Name[i]}");
}
//判断集合是否包括某个元素
bool IsInclud = Name.Contains("名字2");
bool IsInclud1 = Name.Contains("名字12");
Console.WriteLine(#34;这个是存在的输出{IsInclud}");
Console.WriteLine(#34;这个是不存在的输出{IsInclud1}");
//在知道位置插入某个元素
Name.Insert(1, "张三");
foreach (var item in Name)
{
Console.Write(item);
}
Console.WriteLine("以上是插入的");
//移除元素
Name.Remove("张三");//按照元素删除
Name.RemoveAt(0);//按照索引删除
// Name.Clear();这个是全部删除
foreach (var item in Name)
{
Console.Write(item);
}
Console.WriteLine("以上是移除的");
}
}
五、把类添加到泛型并遍历内容
创建类
public class Class1
{
public Class1(){}
public Class1(int id, string name, string description, string type)
{
Id = id;
Name = name;
Description = description;
Type = type;
}
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Type { get; set; }
}
使用类,并用添加到泛型集合遍历
static void Test4()
{
Class1 class1 = new Class1(100,"名字1","学习好","男");
Class1 class2 = new Class1(101, "名字2", "学习中", "女");
Class1 class3 = new Class1(103, "名字3", "学习差", "男");
List<Class1> ListClass=new List<Class1>() { class1,class2,class3};
foreach (var item in ListClass)
{
Console.WriteLine(#34;{item.Id},{item.Name},{item.Description},{item.Type}");
}
}
总结:
以上是泛型常见的用法,多多联系