C#实现Array,List,Dictionary相互转换

作者:農碼一生 时间:2022-10-09 13:51:39 

一、代码实例实现功能

  • 将Array转换为List

  • 将List转换为Array

  • 将Array转换为Dictionary

  • 将Dictionary转换为Array

  • 将List转换为Dictionary

  • 将Dictionary转换为List

二、代码实现

 学生类

class Student
   {
       public int Id { get; set; }
       public string Name { get; set; }
       public string Gender { get; set; }
   }

 转换实现代码

static void Main(string[] args)
       {
           #region 创建学生数组
           //创建数组
           Student[] StudentArray = new Student[3];
           //创建创建3个student对象,并赋值给数组的每一个元素
           StudentArray[0] = new Student()
           {
               Id = 0001,
               Name = "Tony",
               Gender = "M"
           };
           StudentArray[1] = new Student()
           {
               Id = 0002,
               Name = "Hulk",
               Gender = "M"
           };
           StudentArray[2] = new Student()
           {
               Id = 0003,
               Name = "Black",
               Gender = "F"
           };

#endregion
           Console.WriteLine("=================测试打印信息=================");

//打印Array中学生信息
           Console.WriteLine("打印Array中学生信息:");
           foreach (Student student in StudentArray)
           {
               Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + "  " + " Gender = " + student.Gender);
           }

//Array转为LIST
           List<Student> StudentList = StudentArray.ToList<Student>();
           //打印List中的学生信息
           Console.WriteLine("打印List中学生信息:");
           foreach (Student student in StudentList)
           {
               Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
           }

//LIST转为Array
           Student[] ListToArray = StudentList.ToArray<Student>();
           Console.WriteLine("打印ListToArray中的学生信息:");
           //打印ListToArray中的学生信息
           foreach (Student student in ListToArray)
           {
               Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
           }

//Array转换为Dictionary
           Dictionary<int, Student> StudentDictionary = StudentArray.ToDictionary(key => key.Id, Studentobj => Studentobj);
           //打印ArrayToDictionary中的学生信息
           Console.WriteLine("打印ArrayToDictionary中的学生信息:");
           foreach (KeyValuePair<int, Student> student in StudentDictionary)
           {
               Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
           }

//Dictionary转换为Array
           Student[] DictionaryToArray = StudentDictionary.Values.ToArray();
           //打印Dictionary转Array中的学生信息
           Console.WriteLine("打印DictionaryToArray中的学生信息:");
           foreach (Student student in DictionaryToArray)
           {
               Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
           }

//List转换为Dictionary
           Dictionary<int, Student> ListToDictionary = StudentList.ToDictionary(key => key.Id, value => value);
           //打印ListToDictionary中的学生信息
           Console.WriteLine("打印ListToDictionary中的学生信息:");
           foreach (KeyValuePair<int, Student> student in ListToDictionary)
           {
               Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
           }

//Dictionary转换为List
           List<Student> DictionaryToList = StudentDictionary.Values.ToList();
           //打印DictionaryToList中的学生信息
           Console.WriteLine("打印DictionaryToList中的学生信息:");
           foreach (Student student in DictionaryToList)
           {
               Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
           }
           Console.WriteLine("===============END===================");
           Console.ReadLine();
       }

三、结果输出

C#实现Array,List,Dictionary相互转换

来源:https://www.cnblogs.com/wml-it/p/15514456.html

标签:C#,Array,List,Dictionary,相互,转换
0
投稿

猜你喜欢

  • C#事件管理器如何清空所有监听详解

    2022-01-17 14:29:44
  • Java Timezone类常见问题_动力节点Java学院整理

    2023-08-23 01:30:11
  • Spring Security实现HTTP认证

    2021-10-31 14:21:47
  • Java线程中卖火车票问题的深入讲解

    2022-07-22 07:38:52
  • 深入C#中使用SqlDbType.Xml类型参数的使用详解

    2022-07-24 13:56:18
  • java实现文件上传下载

    2023-11-23 09:41:18
  • Spring Cloud Hystrix异常处理方法详解

    2022-05-29 06:20:12
  • c# 插入数据效率测试(mongodb)

    2021-12-16 12:46:07
  • java实现点击按钮事件弹出子窗口

    2023-11-17 14:54:45
  • c#不使用系统api实现可以指定区域屏幕截屏功能

    2022-05-26 13:52:43
  • Netty分布式pipeline管道创建方法跟踪解析

    2023-11-03 02:55:51
  • Java中使用websocket实现在线聊天功能

    2023-01-03 22:07:20
  • Android开发之SeekBar基本使用及各种美观样式示例

    2023-06-30 07:15:22
  • Android实现手指触控图片缩放功能

    2021-06-07 17:08:00
  • MyBatis入门学习教程(一)-MyBatis快速入门

    2021-08-02 05:06:31
  • Java数组的扩容代码示例

    2021-10-10 00:04:21
  • TC 集群Seata1.6高可用架构源码解析

    2022-04-18 05:02:34
  • Java代码块与代码加载顺序原理详解

    2023-06-03 12:56:42
  • java修改JFrame默认字体方式

    2022-11-16 14:02:50
  • java判断两个时间是不是同一天的方法

    2022-09-23 03:09:16
  • asp之家 软件编程 m.aspxhome.com