C#中的集合学习笔记
前言:漫长的国庆长假终于快结束了,我们该工作的工作了,该学习的学习了,该干什么的也都开始了,这篇博客我主要说一下C#中集合的使用,也就是ArrayList集合和Hashtable字典集合的使用,我在这里说的主要还是简单的使用,使学习的能够很快的学习集合然后自己研究集合的一些高级用法,在最后还列举出了一些常用的小案例。
- ArrayList
(1) 命名空间 System.Collection
(2)创建对象
1)增
->Add
->AddRange 任何一个集合对象
int[] num = { 1, 2, 3, 4, 5, 6 };
ArrayList arr = new ArrayList();
//ICollection一个对象里面包含很多其他对象
arr.AddRange(num);
->Insert
2)删
->Remove
->RemoveAt
->Clear
3)改
->像数组一样通过索引来修改
4)查
->Contains
->IndexOf
- 自定义集合
(1) 集合是Object类型,处理数据的时候会需要强转等操作
(2)举例说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 1)新建一个Person类 class Person { public string Name { get ; set ; } public int Age { get ; set ; } public char Gender { get ; set ; } public Person( string n, int a, char g) { Name = n; Age = a; Gender = g; } public void SayHello() { Console.WriteLine( "我叫{0}" , Name); } } 2)新建一个PeresonCollection类 class PersonCollection { ArrayList arr = new ArrayList(); public void Add(Person p) { arr.Add(p); } public void AddRange( params Person[] ps) { arr.AddRange(ps); } public Person this [ int index] { get { return (Person)arr[index]; } } public int Count { get { return arr.Count; } } } 3)在main方法中实现 static void Main( string [] args) { PersonCollection ps = new PersonCollection(); ps.Add( new Person( "韩迎龙" , 23, '男' )); ps.Add( new Person( "韩全龙" , 16, '男' )); ps.Add( new Person( "泛亚内" , 16, '男' )); ps.Add( new Person( "野田" , 16, '男' )); for ( int i = 0; i < ps.Count; i++) { ps[i].SayHello(); } Console.ReadKey(); } |
(3)定义索引
1)就是一个通过传进来的参数访问内部数据的一个属性(带有参数的属性)
2)public 返回类型 this[索引参数类型 参数名]
{
get{return 通过参数访问的数据;}
set{通过参数处理要修改的数据=value}
}
案例说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | class Arr { int [] nums = { 10, 23, 34, 45, 56 }; public int this [ int index] { get { if (index > nums.Length - 1) { throw new Exception( "索引超出异常" ); } return nums[index]; } set { nums[index] = value; } } public int Count { get { return nums.Length; } } } class IndexString { int num = 10; string str = "字符串韩迎龙" ; char ch = 'c' ; public int this [ int index] { get { return num; } } public string this [ string str] { get { return this .str; } } public char this [ char c] { get { return ch; } } public string this [ int n, char c, string s] { get { return num + str + ch; } } } class Program { static void Main( string [] args) { IndexString IS= new IndexString(); Console.WriteLine(IS[ ' ' ]); Console.WriteLine(IS[0, ' ' , " " ]); Console.ReadKey(); } } |
3)模拟实现集合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | class MyCllection { ArrayList arrInt = new ArrayList(); ArrayList arrStr = new ArrayList(); ArrayList obj= new ArrayList(); public void Add( string key, object obj) { int index = arrStr.Add(key); arrInt.Add(index); this .obj.Add(obj); } public object this [ int index] { get { return obj[index]; } } public object this [ string key] { get { return obj[arrStr.IndexOf(key)]; } } } static void Main( string [] args) { MyCllection mc = new MyCllection(); mc.Add( "HYL" , "韩迎龙" ); mc.Add( "HYL" , "韩全龙" ); Console.WriteLine(mc[0]); Console.WriteLine(mc[ "HYL" ]); Console.ReadKey(); } |
- foreach语法
(1) 执行
1)可以只遍历一部分吗? 不能
2)可以倒着遍历吗? 不能
3)举例说明:
int[] nums = { 1, 4, 6, 87, 87, 3, 23, 45 };
foreach (int item in nums)
{
Console.WriteLine(item);
}
Console.ReadKey();
(2)数据源应该是一个可枚举的类型(IEnumerable)
(3)foreach循环的步骤
1)foreach循环首先到数据集中调用GetEnumerator方法得到枚举器对象
2)到in关键字,调用MoveNext()方法
3)这个方法返回bool值,将枚举器移到数据集的下一个数据上,并检查是否有数据,如果有,返回true,否则返回false
4)到临时变量(迭代变量),开始调用枚举器的Current属性,将当前的数据取出来,然后赋值给临时变量
5)就可以使用了
- 嵌套类
(1) 有时候在某个类型中需要一个特定的类型处理数据,而这个类型只在当前对象类中可使用
(2)可以将这个类定义在一个类的里面,此时称为嵌套类
(3)嵌套类的访问修饰符和一般成员一样
(4)一般是为了考虑安全问题
- yield迭代器
(1) 一般的语法:yield return 数据
(2)foreach在执行开始访问数据集的时候,依旧是调用IEnumerable中的GetEnumerator方法
(3)这个Getenumerator方法可以使用这个迭代器返回数据
(4)在方法中写上循环,循环体上放上yield,那么循环不会立即结束
(5)每当执行到yield的时候就会停在那里,跳到foreach中
(6)foreach执行到in的时候,又会跳到上次停留的yield那里,继续向下执行
(7)直到下一个yield,再次调用foreach
public IEnumerator GetEnumerator()
{
for(int i=0;i<arr.Count;i++)
{
yield return arr[i];
}
}
- Hashtable 字典集合
(1) 举例说明:
Hashtable has = new Hashtable();
has.Add("BigBoss", "韩迎龙");
has.Add("sdd", "的事实");
has.Add("dfd", "的速度");
Console.WriteLine(has["BigBoss"]);
(2)Hashtable的使用,实现一个电子词典
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | class Program { static void Main( string [] args) { string [] words = File.ReadAllLines( "英汉词典TXT格式.txt" , Encoding.Default); Hashtable has = new Hashtable(); for ( int i = 0; i < words.Length; i++) { string [] temp = words[i].Split( new char [] { ' ' }, StringSplitOptions.RemoveEmptyEntries); if (temp.Length == 1) { continue ; } string key = temp[0]; string value = string .Join( " " , temp, 1, temp.Length - 1); if (!has.ContainsKey(key)) { has.Add(key, value); } else { has[key] = string .Format( "{0}\n{1}" , has[i], value); } } //可以查看了 while ( true ) { Console.Write( "请输入单词?" ); string str=Console.ReadLine(); if (has.ContainsKey(str)) { Console.WriteLine(has[str]); } else { Console.WriteLine( "很抱歉,你说查询的词汇没有找到" ); } } Console.ReadKey(); } } static void Main( string [] args) { string [] words = File.ReadAllLines( "英汉词典TXT格式.txt" , Encoding.Default); Hashtable has = new Hashtable(); for ( int i = 0; i < words.Length; i++) { string [] temp = words[i].Split( new char [] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string key = temp[0]; string value = temp[1]; if (temp.Length != 2) { continue ; } if (!has.ContainsKey(key)) { has.Add(key, value); } else { has[key] = string .Format( "{0}\n{1}" , has[i], value); } } //可以查看了 while ( true ) { Console.Write( "请输入单词?" ); string str=Console.ReadLine(); if (has.ContainsKey(str)) { Console.WriteLine(has[str]); } else { Console.WriteLine( "很抱歉,你说查询的词汇没有找到" ); } } Console.ReadKey(); } } |
(3)如何遍历?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | static void Main( string [] args) { Hashtable has = new Hashtable(); has.Add( "1" , "韩迎龙" ); has.Add( "2" , "得到" ); //仅仅遍历key foreach ( string item in has.Keys) { Console.WriteLine(item); } //遍历value foreach ( string item in has.Values) { Console.WriteLine(item); } //遍历整个集合 foreach (DictionaryEntry item in has) { Console.WriteLine( "{0}是{1}" , item.Key, item.Value); ; } Console.ReadKey(); } |
(4)var 类型推断
1)var val=数据;
2)赋什么数据就是什么类型
3)类型确定以后就不能变了
4)必须保证类型赋值时时确定的
5)何时使用类型推断
->匿名类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | static void Main( string [] args) { var person = new { Name = "张三" , Age = 23, Gender = '男' }; Console.WriteLine(person.Name); Console.WriteLine(person.Age); Console.WriteLine(person.Gender); Console.ReadKey(); } |
->Linq查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | static void Main( string [] args) { int [] nums = { 132, 454, 667, 343, 65, 76, 74 }; var query = from n in nums where n > 100 && n < 500 select n; foreach ( var item in query) { Console.WriteLine(item); } Console.ReadKey(); } |
初心商城:初心商城
作者:韩迎龙(Kencery) MVC/.NET群:159227188如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏一杯咖啡,本页版权归作者和博客园所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构