C#实现Array,List,Dictionary的相互转换
C#实现Array,List,Dictionary的相互转换
一、代码实例实现功能
- 将Array转换为List
- 将List转换为Array
- 将Array转换为Dictionary
- 将Dictionary转换为Array
- 将List转换为Dictionary
- 将Dictionary转换为List
二、代码实现
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Student { public int Id { get; set; } public string Name { get; set; } public string Gender { get; set; } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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(); }
三、结果输出
本文来自博客园,作者:码农阿亮,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/15514456.html
技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
开源库地址,欢迎点亮:
GitHub:https://github.com/ITMingliang
Gitee: https://gitee.com/mingliang_it
GitLab: https://gitlab.com/ITMingliang
建群声明: 本着技术在于分享,方便大家交流学习的初心,特此建立【编程内功修炼交流群】,为大家答疑解惑。热烈欢迎各位爱交流学习的程序员进群,也希望进群的大佬能不吝分享自己遇到的技术问题和学习心得!进群方式:扫码关注公众号,后台回复【进群】。
![](https://images.cnblogs.com/cnblogs_com/wml-it/2196906/o_230823011905_wxgzhss.png)