ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【原创】C#零基础学习笔记008-C#集合处理

2021-03-31 23:03:38  阅读:199  来源: 互联网

标签:hash C# System 笔记 集合 Add 泛型 using 008


其他路径:

CSDN: https://blog.csdn.net/wodehao0808/article/details/115257928

微信公众号:程序喵星人

 

8 C#集合的处理
8.1 哈希表集合
       Hashtable集合是键/值对的集合。

  DictionaryEntry类型的实例,DictionaryEntry类型有一个key和value属性来读取和设置键和值。

  动态存放键/值对。

  键值是唯一的,不可重复。

 

8.2 Example: 集合处理:Hashtable表
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; // 引入 hash 所在的命名空间

// 集合处理:Hashtable表

namespace Lesson_35_1
{
class Program
{
static void Main(string[] args)
{
Hashtable hash = new Hashtable();
// 往hash里添加数据
hash.Add( 1, "hello" );
hash.Add( 2, "world" );
hash.Add( 3, "C#" );
// 访问hash里的数据,通过hash访问的时候,我们采用的是键这种方式访问
Console.WriteLine( hash[1] );
// 遍历方式1:可以采用遍历它的键集合
var keys = hash.Keys; // hash键的集合
foreach (object key in keys)
{
Console.WriteLine("键:{0},值:{1}", key, hash[key]); // 输出顺序跟Add顺序相反
}
// 遍历方式2:使用遍历器(迭代器)
var ie = hash.GetEnumerator(); // 获取一个遍历器
while (ie.MoveNext()) // 依次遍历集合中的数据,类似于游标
{
Console.WriteLine("遍历器--键:{0},值:{1}", ie.Key, ie.Value);
}
}
}
}
8.3 泛型的应用
  泛型(generic)是C#语言2.0和通用语言运行时(CLR)的一个新特性。

  在泛型类型或泛型方法的定义中,类型参数是一个占位符(placeholder),通常为一个大写字母。

8.4 字典集合
  键值对的存储,这种键值对的存储可以使用泛型。即,字典。

 

8.5 Example: 集合:泛型应用和字典集合
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic; // 如果要使用泛型,必须引入这个命名空间

// 集合:泛型应用和字典集合

namespace Lesson_36_1
{
class Program
{
static void Main(string[] args)
{
// 数据类型不安全:即数据的类型是不确定的。
// 在使用ArrayList无法保证类型的一致性,例如:
ArrayList arrList = new ArrayList();
arrList.Add(1); // int类型
arrList.Add("Hello"); // string 类型
arrList.Add(true); // bool 类型
// 以上,arrList的类型是不确定的,不是固定的,所以数据类型不安全。

// 泛型的特点就是类型安全,类型可以指定为固定类型。
// 泛型规定了在集合中所存储的数据的类型。
List<int> list = new List<int>(); // 指定list的类型是int
list.Add(1);
list.Add(2);
list.Add(3);
// 泛型遍历
foreach (int i in list )
{
Console.WriteLine("泛型foreach遍历,输出的数据:" + i); // 输出的顺序是add的顺序
}
for (int i = 0; i < list.Count; ++i )
{
Console.WriteLine("泛型for遍历,输出的数据:" + list[i]); // 输出的顺序是add的顺序
}

// 字典集合的存储
Dictionary<string, string> dic = new Dictionary<string, string>(); // 指定了 键的类型为string 和 值的类型为string
dic.Add("1001", "张三");
dic.Add("1002", "李四");
dic.Add("1003", "王五");
// 采用类似于访问hash表的方式去访问字典集合的数据
var keys = dic.Keys;
foreach (string key in keys )
{
Console.WriteLine("字典采用foreach遍历keys方法输出,键:{0},值:{1}", key, dic[key]); // 输出的顺序就是add的顺序
}
var ie = dic.GetEnumerator();
while (ie.MoveNext())
{
// 注意,这里 ie.Current 后,再 .key 和 .Value。
Console.WriteLine("字典采用遍历器GetEnumerator方法输出,键:{0},值:{1}", ie.Current.Key, ie.Current.Value); // 输出的顺序就是add的顺序
}
}
}
}
 
————————————————
版权声明:本文为CSDN博主「公众号:程序喵星人」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wodehao0808/article/details/115257928

标签:hash,C#,System,笔记,集合,Add,泛型,using,008
来源: https://www.cnblogs.com/wodehao0808/p/14604179.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有