C#数组,List,Dictionary的相互转换
本篇文章会向大家实例讲述以下内容:
- 将数组转换为List
- 将List转换为数组
- 将数组转换为Dictionary
- 将Dictionary 转换为数组
- 将List转换为Dictionary
- 将Dictionary转换为List
首先这里定义了一个“Student”的类,它有三个自动实现属性。
class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
将数组转换为List
将数组转换成一个List,我先创建了一个student类型的数组。
static void Main (string[] args)
{
//创建数组
Student[] StudentArray = new Student[];
//创建创建3个student对象,并赋值给数组的每一个元素 StudentArray[0] = new Student()
{
Id = ,
Name ="Tony Stark",
Gender ="Male"
};
StudentArray[] = new Student()
{
Id = ,
Name="Hulk",
Gender = "Male"
};
StudentArray[] = new Student()
{
Id = ,
Name ="Black Widow",
Gender="Female"
};
接下来,使用foreach遍历这个数组。
foreach (Student student in StudentArray)
{
Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender);
}
运行程序

接下来将这个数组转换为List,我们添加System.Linq命名空间,然后调用ToList()扩展方法。这里我们就调用StudentArray.ToList()
注意这个ToList方法的返回类型,它返回的是List< Student >对象,这说明我们可以创建一个该类型的对象来保存ToList方法返回的数据。
List<Student> StudentList = StudentArray.ToList<Student>();
使用foreach从StudentList中获取所有的学生资料。
List<Student> StudentList = StudentArray.ToList<Student>(); foreach (Student student in StudentList)
{
Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender);
}
运行程序

将List转换为数组
将List转换为数组,使用System.Linq命名空间下的ToArray()扩展方法。
Student[] ListToArray = StudentList.ToArray<Student>();
使用foreach遍历学生资料
foreach (Student student in ListToArray)
{
Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender);
}
运行程序
 
将数组转换为Dictionary
将数组转换成Dictionary,使用ToDictionary()扩展方法。这里就可以用StudentArray.ToDictonary(
看这个方法需要的参数,第一个参数需要键和第二个参数需要值。我们知道Dictionary是一个泛型,它是键/值对类型的集合。因此,这里我们用一个lambda表达式传递Dictionary对象名称。
StudentArray.ToDictionary(key => key.Id,Studentobj => Studentobj);
这个ToDictionary方法返回的类型是Dictionary 对象。 其键/值对<int,Student>类型,同样说明我们可以创建一个该类型的对象来存储ToDictionary方法得到的数据。
Dictionary<int, Student> StudentDictionary = StudentArray.ToDictionary(key => key.Id,Studentobj => Studentobj);
使用foreach从这个StudentDictionary对象遍历学生资料,如下:
foreach (KeyValuePair<int, Student> student in StudentDictionary)
{
Console.WriteLine("Id = "+student.Key+" "+" Name = "+student.Value.Name+" "+" Gender = "+student.Value.Gender);
}
运行程序

将Dictionary转换为数组
将Dictionary转换成数组,使用ToArray扩展方法。在之前,需要获取Dictionary对象的集合中的值,所以我们使用Values属性的ToArray方法。
Student[] DictionaryToArray = StudentDictionary.Values.ToArray();
使用foreach遍历学生资料
foreach (Student student in DictionaryToArray)
{
Console.WriteLine("Id = "+student.Id+" "+" Name = " +student.Name+" "+" Gender = "+student.Gender);
}
运行程序

将List转换为Dictionary
之前已经创建了一个StudentList学生对象,将StudentList转换为Dictionary我们调用ToDictionary方法。
Dictionary<int, Student> ListToDictionary = StudentList.ToDictionary(key => key.Id, value => value);
对于ToDictionary方法的两个参数,我们分别通过键和值传递其对象。这里ToDictionary被赋值,并返回了一个< int,Student >Dictionary 对象。所以我们创建该类型的对象然后存储返回的数据,最后用foreach获取学生资料。
foreach (KeyValuePair<int,Student> student in ListToDictionary)
{
Console.WriteLine("Id = "+student.Key+" "+" Name = " +student.Value.Name+" "+" Gender = "+student.Value.Gender);
}
运行程序

将Dictionary转换为List
将Dictionary 转换成List调用ToList方法,之前已经创建了一个StudentDictionary对象。直接看如何这个对象转换到list.
List<Student> DictionaryToList = StudentDictionary.Values.ToList();
foreach (Student student in DictionaryToList)
{
Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender);
}
运行程序

希望本文对你有帮助 
C#数组,List,Dictionary的相互转换的更多相关文章
- [Swift]JSON字符串与字典(Dictionary)、数组(Array)之间的相互转换
		1.JSON字符串与字典(Dictionary)之间的相互转换 import Foundation //JSON字符串转换为字典(Dictionary) func getDictionaryFromJ ... 
- C#数组,List,Dictionary,IQueryable,IEnumerable的相互转换
		本篇文章会向大家实例讲述以下内容: 将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dicti ... 
- 一个Json、数组、Dictionary转换和数组对比的C#实例
		最近做了一个程序,里面一段代码用到Json.数组.Dictionary转换和数组对比的一些知识,虽然在实际碰到类似问题时候有更好的方法,但这就当是一次基础知识的回顾,现在分享一下. 先介绍下要实现的业 ... 
- Java中数组与集合的相互转换
		数组与List的相互转换 List转数组:采用集合的toArray()方法 数组转List:采用Arrays的asList()方法 数组转换为集合 注意:在数组转集合的过程中,要注意是否使用了视图的方 ... 
- iOS-NSdata 与 NSString,Byte数组,UIImage 的相互转换
		IOS---NSdata 与 NSString,Byte数组,UIImage 的相互转换 1. NSData 与 NSString NSData-> NSString NSString *aSt ... 
- 如何实现数组与List的相互转换?在 Queue 中 poll()和 remove()有什么区别?哪些集合类是线程安全的?
		如何实现数组与List的相互转换? List转数组:toArray(arraylist.size()方法 数组转List:Arrays的asList(a)方法 /** * 〈一句话功能简述〉; * 〈 ... 
- Swift3 - String 字符串、Array 数组、Dictionary 字典的使用
		Swift相关知识,本随笔为 字符串.数组.字典的简单使用,有理解.使用错误的地方望能指正. ///************************************************** ... 
- C# byte数组与Image的相互转换
		功能需求: 1.把一张图片(png bmp jpeg bmp gif)转换为byte数组存放到数据库. 2.把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示. 3.从图片byte ... 
- js数组与字符串的相互转换方法
		一.数组转字符串 需要将数组元素用某个字符连接成字符串,示例代码如下: var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); 二 ... 
随机推荐
- [C#] async 的三大返回类型
			async 的三大返回类型 序 博主简单数了下自己发布过的异步文章,已经断断续续 8 篇了,这次我想以 async 的返回类型为例,单独谈谈. 异步方法具有三个可让开发人员选择的返回类型:Task&l ... 
- 菜鸟Python学习笔记第一天:关于一些函数库的使用
			2017年1月3日 星期二 大一学习一门新的计算机语言真的很难,有时候连函数拼写出错查错都能查半天,没办法,谁让我英语太渣. 关于计算机语言的学习我想还是从C语言学习开始为好,Python有很多语言的 ... 
- sublime常用快捷键
			自己觉得比较实用的sublime快捷键: Ctrl + / ---------------------注释 Ctrl + 滚动 --------------字体变大/缩小 Ctrl + N----- ... 
- Webstorm+Webpack+echarts构建个性化定制的数据可视化图表&&两个echarts详细教程(柱状图,南丁格尔图)
			Webstorm+Webpack+echarts ECharts 特性介绍 ECharts,一个纯 Javascript 的图表库,可以流畅的运行在 PC 和移动设备上,兼容当前绝大部分浏览器(I ... 
- CSS 选择器及各样式引用方式
			Css :层叠样式表 (Cascading Style Sheets),定义了如何显示HTML元素. 目录 1. 选择器的分类:介绍ID.class.元素名称.符合.层次.伪类.属性选择器. 2. 样 ... 
- redux-undo
			简介 通过包装reducer,创建一个state History,保留历史state,可以做退一步,进一步操作 1.install npm install --save redux-undo@beta ... 
- ASP.NET Core应用针对静态文件请求的处理[3]: StaticFileMiddleware中间件如何处理针对文件请求
			我们通过<以Web的形式发布静态文件>和<条件请求与区间请求>中的实例演示,以及上面针对条件请求和区间请求的介绍,从提供的功能和特性的角度对这个名为StaticFileMidd ... 
- SQLServer 版本之八大方法搞清 "我是谁"
			你正在使用 SQL Server 的哪个版本? 贴士:作为一个SQL Server数据库管理者或维护.支持人员,应该会经常问自己这样一个问题:我当前SQL Server版本号是?当前版本已经有的累计更 ... 
- 基于RN开发的一款视频配音APP(开源)
			在如今React.ng.vue三分天下的格局下,不得不让自己加快学习的脚步.虽然经常会陷入各种迷茫,学得越多会发现不会的东西也被无限放大,不过能用新的技术作出一些小项目小Demo还是会给自己些许自信与 ... 
- H3 BPM让天下没有难用的流程之技术体系
			一.技术架构 H3 BPM 基于微软.NET 技术架构,采用C#语言开发,以高开放.高扩展.高性能为核心准则,遵循分层的设计原理,结合最新的B/S 以及智能手机应用开发技术研发的. 图:H3 BPM ... 
