一、集合:专门用来数组存储和检索
泛型集合:List(通过索引访问元素)、Dictionary(通过key访问元素)、
非泛型集合:ArrayList(通过索引访问元素)、Hashtable(通过key访问元素)、BitArray(通过整型索引访问)
既有泛型又有非泛型:SortedList(通过索引和key访问元素)、Stack、Queue
1.ArrayList:动态数组。当中间元素被删除时,后面的元素会前移
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CollectionTest
{
internal class Program
{
static void Main(string[] args)
{
ArrayList arrayList = new ArrayList();
//常用方法
arrayList.Add(123);//添加元素
arrayList.Insert(1, "动态数组");//在指定索引处插入元素
arrayList.Remove(123);//移除元素
arrayList.RemoveAt(0);//移除指定索引处的元素
Console.WriteLine(arrayList.Contains("动态数组"));//判断是否包含某元素
Console.WriteLine(arrayList.IndexOf("动态数组"));//获取某元素的索引
arrayList.Reverse();//逆置动态数组中的元素
arrayList.Sort();//对动态数组排序
arrayList.Clear();//清空元素
//arraylist.ToArray(); 将arraylist转为数组
//常用属性
int num=arrayList.Count;//动态数组中实际的元素个数
arrayList[0] = 123;//获取或设置指定索引的元素
}
}
}
更多内容:C# 动态数组(ArrayList) | 菜鸟教程
2.List:链表。当中间元素被删除时,后面的元素会前移
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CollectionTest
{
internal class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>();
//常用方法
list.Add(123);//添加元素
list.Insert(1, 234);//在指定索引处插入元素
list.Remove(123);//移除元素
list.RemoveAt(0);//移除指定索引处的元素
Console.WriteLine(list.Contains(234));//判断是否包含某元素
Console.WriteLine(list.IndexOf(234));//获取某元素的索引
list.Reverse();//逆置动态数组中的元素
list.Sort();//对动态数组排序
list.Clear();//清空元素
//list.ToArray();转为数组
//常用属性
int num=list.Count;//动态数组中实际的元素个数
list[0] = 123;//获取或设置指定索引的元素
}
}
}
更多详情:C# List(列表) - C#教程 - 菜鸟教程
3.SortedList:排序列表。它是数组与哈希表的组合,我个人更倾向于它是个特殊的哈希表,只是比哈希表多出了索引的访问元素的方式。
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CollectionTest
{
internal class Program
{
static void Main(string[] args)
{
SortedList sl=new SortedList();
//SortedList<int, string> sl2 = new SortedList<int, string>();
//常用方法
sl.Add(123,"排序列表");//添加key和value
sl.ContainsKey(123);//判断是否包含某个key
sl.ContainsValue("排序列表");//判断是否包含某个value
sl.GetByIndex(0);//获取指定索引的value
sl.GetKey(0);//获取指定索引的key
sl.IndexOfKey(123);//获取指定key的索引,索引从0开始
sl.IndexOfValue("排序列表");//获取指定value的索引
sl.GetKeyList();//获取所有key
sl.GetValueList();//获取所有value
sl.Remove(123);//移除指定key的键值
sl.RemoveAt(0);//移除指定索引的键值
sl.Clear();//清空所有键值
//常用属性
int num = sl.Count;//键值总数
//sl.Keys 表示sl中的所有key
//sl.Values 表示sl中的所有value
sl[0] = "排序列表"; //获取或设置指定键的值
}
}
}
更多内容:https://www.runoob.com/csharp/csharp-sortedlist.html
4.Hashtable:哈希表。一个key对应一个value,key是唯一的,键值对没有索引,它用key来表示索引。使用foreach遍历,而不用for
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CollectionTest
{
internal class Program
{
static void Main(string[] args)
{
Hashtable hash = new Hashtable();
//常用方法
hash.Add(200, "键值对");//添加带有指定key和value的元素
hash.ContainsKey(200);//判断是否包含指定key
hash.ContainsValue("键值对");//判断是否包含指定value
hash.Remove(200);//通过key移除value
hash.Clear();//清空所有
//常用属性
hash[200] = "获取或设置";//通过key获取或设置value
//hash.Keys 表示hash中的所有key
//hash.Values 表示hash中的所有value
//hash.Count 表示hash中的元素个数
}
}
}
5.Dictionary:字典。与哈希表类似
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CollectionTest
{
internal class Program
{
static void Main(string[] args)
{
Dictionary<int, string> dic = new Dictionary<int,string>();
//常用方法
dic.Add(200, "键值对");//添加带有指定key和value的元素
dic.ContainsKey(200);//判断是否包含指定key
dic.ContainsValue("键值对");//判断是否包含指定value
dic.Remove(200);//通过key移除value
dic.Clear();//清空所有
//常用属性
dic[200] = "获取或设置";//通过key获取或设置value
//dic.Keys 表示dic中的所有key
//dic.Values 表示dic中的所有value
//dic.Count 表示dic中的元素个数
//字典排序:以value值排序
dic=dic.OrderByDescending(r=>r.Value).ToDictionary(r=>r.Key,r=>r.Value);//降序
dic = dic.OrderBy(r => r.Value).ToDictionary(r => r.Key, r => r.Value);//升序
}
}
}
更多内容:C# Dictionary用法汇总(定义、遍历、排序)
6.Stack:栈(先进后出,后进先出)
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CollectionTest
{
internal class Program
{
static void Main(string[] args)
{
Stack stack= new Stack();
//Stack<int> stack2 = new Stack<int>();
//常用方法
stack.Push("入栈");//元素入栈
Object obj= stack.Peek();//查找并返回栈顶元素,而不删除
stack.Contains("队列");//判断是否包含某个元素
Object obj2= stack.Pop();//元素出栈并返回出栈元素
stack.Clear();//清空队列
//常用属性
int num=stack.Count;//元素个数
}
}
}
更多内容:C# 堆栈(Stack) | 菜鸟教程
7.Queue:队列(先进先出)
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CollectionTest
{
internal class Program
{
static void Main(string[] args)
{
Queue queue = new Queue();
//Queue<int> queue2=new Queue<int>();
//常用方法
queue.Enqueue("队列");//元素入队
Object obj= queue.Peek();//查找并返回队首元素,而不取出
queue.Contains("队列");//判断是否包含某个元素
queue.Dequeue();//元素出队
queue.Clear();//清空队列
//常用属性
int num=queue.Count;//元素个数
}
}
}
更多内容:C# 队列(Queue) | 菜鸟教程
8.BitArray:点阵列。使用1和0来表示的二进制数组,其中true表示位是开启的(1),false表示位是关闭的(0)
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CollectionTest
{
internal class Program
{
static void Main(string[] args)
{
//创建点阵列的时候要指定大小
BitArray ba = new BitArray(8);
//创建byte,并放入数据(并非一定要用byte)
byte[] a = { 32 };
//如果使用int型数组,则下面语句将存放数组到点阵列中时,会直接扩大点阵列
//如果使用byte数组,但是多加几个数据也会扩大点阵列
ba = new BitArray(a);
for(int i=0;i<ba.Count;i++)
{
Console.WriteLine(ba[i]);
//打印的是数组元素的二进制(以true和false表示)
}
}
}
}
二、泛型:最大限度的重用代码、保护类型的安全以及提高性能。可以创建泛型接口、泛型类、泛型方法、泛型委托,并约束泛型的数据类型
using System;
using System.Collections.Generic;
namespace GenericTest
{
delegate T DelegateTest<T>(T ele);//泛型委托
internal class Program
{
static Test<T> UseGeneric<T>(T addOfValue1, T addOfValue2)//泛型方法
{
Test<T> test=new Test<T>();//创建Test<T>泛型类
DelegateTest<T> delegateTest = new DelegateTest<T>(test.AddElement);//使用委托注册方法
delegateTest(addOfValue1);//添加元素
delegateTest(addOfValue2);//添加元素
return test; //返回Test<T>类
}
static void Main(string[] args)
{
Test<int> testInt= UseGeneric<int>(2, 3);
testInt.Show(); //输出2,3
Test<char> testChar=UseGeneric<char>('a', 'b');
testChar.Show(); //输出a,b
Test<bool> testBool= UseGeneric<bool>(true, false);
testBool.Show(); //输出true,false
Test<string> testString= UseGeneric<string>("使用", "泛型");
testString.Show(); //输出 使用,泛型
}
}
//泛型类中不应该写泛型方法和泛型委托
public class Test<T>//泛型类
{
List<T> list=new List<T>();//泛型列表
public T AddElement(T element)//使用了泛型的方法,而非泛型方法
{
list.Add(element);
return element;
}
public void Show()
{
foreach(T element in list)
{
Console.WriteLine(element);
}
}
}
}
约定泛型:
namespace GenericTest
{
internal class Program
{
static void Main(string[] args)
{
//where如果约束为class,则T需要是引用类型
//where如果约束为struct,则T需要是值类型
//where如果约束为定义类,则T一般是该定义类或子类
//where如果约束为new(),则T需要是类,其构造函数需要是公有无参的
//where如果约束为定义接口,则T可以是该定义接口(接口可以带泛型)、实现接口的类
Test<string,Test2,int,Test2,iTest<int>> test = new Test<string,Test2,int,Test2, iTest<int>>();
test.t2 = new ChildTest2();//里氏转换
}
}
public class Test<T1,T2,T3,T4,T5> where T1 : class where T2:Test2 where T3 : struct where T4:new() where T5:iTest<int>
{
public T1 t1;
public T2 t2;
public T3 t3;
public T4 t4;
public T5 t5;
}
public class Test2:iTest<int>
{ }
public class ChildTest2:Test2
{ }
public interface iTest<T> { }
}
看到这里了,不点个👍再走吗😀
结语:合抱之木,生于毫末;九层之台,起于累土;千里之行,始于足下。