C#数组,List,Dictionary的相互转换

简介: 本篇文章会向大家实例讲述以下内容: 将数组转换为List将List转换为数组将数组转换为Dictionary将Dictionary 转换为数组将List转换为Dictionary将Dictionary转换为List首先这里定义了一个“Student”的类,它有三个自动实现属性。

本篇文章会向大家实例讲述以下内容: 

  • 将数组转换为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);
}

运行程序

希望本文对你有帮助

 

目录
相关文章
|
存储 C# 索引
C# 一分钟浅谈:数组与集合类的基本操作
【9月更文挑战第1天】本文详细介绍了C#中数组和集合类的基本操作,包括创建、访问、遍历及常见问题的解决方法。数组适用于固定长度的数据存储,而集合类如`List<T>`则提供了动态扩展的能力。文章通过示例代码展示了如何处理索引越界、数组长度不可变及集合容量不足等问题,并提供了解决方案。掌握这些基础知识可使程序更加高效和清晰。
343 6
|
人工智能 Java
Java 中数组Array和列表List的转换
本文介绍了数组与列表之间的相互转换方法,主要包括三部分:1)使用`Collections.addAll()`方法将数组转为列表,适用于引用类型,效率较高;2)通过`new ArrayList&lt;&gt;()`构造器结合`Arrays.asList()`实现类似功能;3)利用JDK8的`Stream`流式计算,支持基本数据类型数组的转换。此外,还详细讲解了列表转数组的方法,如借助`Stream`实现不同类型数组间的转换,并附带代码示例与执行结果,帮助读者深入理解两种数据结构的互转技巧。
1018 1
Java 中数组Array和列表List的转换
|
存储 API C#
C#之 Dictionary 详解
C#之 Dictionary 详解
315 3
如何实现数组和 List 之间的转换?
如何实现数组和 List 之间的转换?
207 0
|
存储 安全 编译器
Python学习日记(一:List、Tuple、dictionary)
1.列表、元组和字典都是序列 2.列表字典可以修改和删除序列中的某个元素,而元组就是一个整体,不能修改和删除,一定要修改或删除的话,只能修改和删除整个元组。 3.既然元组不能删除和修改,有什么作用呢? 1.元组比列表遍历速度快,因为元组是一个整体,运算效率高; 2.正是因为不能修改,元组可以保护不需要修改的数据,可以使代码结构更安全。
277 2
|
存储 NoSQL 安全
Redis第六弹-List列表-(相当于数组/顺序表)Lpush key element-一次可以插入多个元素(假如key已经存在,并且key对应的value并非是list,则会报错)
Redis第六弹-List列表-(相当于数组/顺序表)Lpush key element-一次可以插入多个元素(假如key已经存在,并且key对应的value并非是list,则会报错)
|
存储 开发框架 .NET
C#中的数组探索
C#中的数组探索
219 0
|
存储 Dart
Dart中的集合类型:List(数组/列表)
Dart中的集合类型:List(数组/列表)
527 0
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
2168 1
|
运维 关系型数据库 Java
PolarDB产品使用问题之使用List或Range分区表时,Java代码是否需要进行改动
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。