温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-04-24 10:20:35 来源:亿速云 阅读:240 作者:iii 栏目:开发技术

这篇文章主要介绍“C#怎么实现Array,List,Dictionary相互转换”,在日常操作中,相信很多人在C#怎么实现Array,List,Dictionary相互转换问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#怎么实现Array,List,Dictionary相互转换”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、代码实例实现功能

  • 将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相互转换

到此,关于“C#怎么实现Array,List,Dictionary相互转换”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI