C#数组中List, Dictionary的相互转换问题
作者:mrr 发布时间:2022-11-03 00:10:37
本篇文章会向大家实例讲述以下内容:
将数组转换为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];
//创建创建3个student对象,并赋值给数组的每一个元素 StudentArray[0] = new Student()
{
Id = 203,
Name ="Tony Stark",
Gender ="Male"
};
StudentArray[1] = new Student()
{
Id = 205,
Name="Hulk",
Gender = "Male"
};
StudentArray[2] = new Student()
{
Id = 210,
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);
}
运行程序
以上所述是小编给大家介绍的#数组中List, Dictionary的相互转换问题网站的支持!


猜你喜欢
- 类加载是什么把磁盘中的java文件加载到内存中的过程叫做类加载当我们用java命令运行某个类的main函数启动程序时,首先需要通过类加载器把
- 本文实例讲述了Android编程开发之在Canvas中利用Path绘制基本图形的方法。分享给大家供大家参考,具体如下:在Android中绘制
- 背景今天面试字节算法岗时被问到的问题,让我用C++实现一个softmax函数。softmax是逻辑回归在多分类问题上的推广。大概的公式如下:
- WPF 实现步骤控件框架使用.NET40;Visual Studio 2019;Step 继承 ItemsControl
- 在学习获取相册中图片进行裁剪的时候遇到了比较大的问题,在纠结了近半天才真的解决,下面分享一下学习经验。问题:选择了相册中的图片之后要进入图片
- 目录操作创建目录File类中有两个方法可以用来创建文件夹:mkdir( )方法创建一个文件夹,成功则返回true,失败则返回false。失败
- 本文实例讲述了C#数字图像处理之图像缩放的方法。分享给大家供大家参考。具体如下://定义图像缩放函数private static Bitma
- 为了演示光照效果,在前面学习过的内容基础上我们首先创建一个立方体,同时为了看起来直观一些,这个立方体每个面采用中心为白色,周围红色的渐变方案
- 前言在开发过程中,使用模板引擎是很有必要的。jsp已经明显跟不上时代发展了,freemarker用的够够的?换thymeleaf试试吧。sp
- C# 从枚举值获取对应的文本描述详解有时枚举值在显示时,需要显示枚举值对应的文本串。一种方案是在调用的地方使用switch或者if来判断枚举
- 五子棋游戏(Java),供大家参考,具体内容如下思路:1.首先创建一个棋盘,建立一个二维数组,此文中为一个15*15的二维数组,2.初始化棋
- JavaFXJavaFX 是一个开源的下一代客户端应用平台,适用于基于Java构建的桌面、移动端和嵌入式系统。 它是许多个人和公司的共同努力
- 在用unity进行游戏开发时我们有时需要一些物体在场景切换时不需要被销毁这时我们可以用官方给的DontDestroyOnLoad()方法,这
- 有时候在配置中心有些参数是需要修改的,这时候如何不重启而达到实时生效的效果呢?添加依赖<dependencies>
- 本文研究的主要内容是Java编程二项分布的递归和非递归实现,具体如下。问题来源:算法第四版 第1.1节 习题27:return (1.0 -
- 本文实例为大家分享了java实现随机数生成器的具体代码,供大家参考,具体内容如下自己编的随机数生成器,比较简陋,功能也单一,当作练手。App
- 上一节我们了解了Lock接口的一些简单的说明,知道Lock锁的常用形式,那么这节我们正式开始进入JUC锁(java.util.concurr
- 本文实例为大家分享了Android学习笔记之蓝牙功能的具体代码,供大家参考,具体内容如下蓝牙:短距离无线通讯技术标准。蓝牙协议分为4层,即核
- 现实开发中,我们难免遇到跨域问题,以前笔者只知道jsonp这种解决方式,后面听说spring只要加入@CrossOrigin即可解决跨域问题
- 本文实例为大家分享了Chronometer实现倒计时功能,Android提供了实现按照秒计时的API,供大家参考,具体内容如下一、自定义Ch