C# List, Array, Dictionary相互转换

Posted luckyzli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# List, Array, Dictionary相互转换相关的知识,希望对你有一定的参考价值。

  • 将Array转换为List
  • 将List转换为Array
  • 将Array转换为Dictionary
  • 将Dictionary 转换为Array
  • 将List转换为Dictionary
  • 将Dictionary转换为List
    首先,我们定义一个“Student”类,它有三个自动实现属性。
    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
    }        

1. Array转换为List

将数组转换成一个List,我先创建了一个student类型的数组。

        static void Main(string[] args)
        {
            Student[] studentArray = new Student[3];
            studentArray[0] = new Student()
            {
                Id = 12,
                Name = "Lucky",
                Gender = "Male"
            };
            studentArray[1] = new Student()
            {
                Id = 23,
                Name = "Poppy",
                Gender = "Male"
            };
            studentArray[2] = new Student()
            {
                Id = 55,
                Name = "Jack",
                Gender = "Female"
            };
            //foreach循环数组
            foreach (Student student in studentArray)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + "  " + " Gender = " + student.Gender);
            }                

技术图片

            //1. 数组转成List
            List<Student> StudentList = studentArray.ToList<Student>();
            foreach (Student student in StudentList)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }

技术图片

2. List转换为Array

            Student[] ListToArray = StudentList.ToArray<Student>();
            foreach (Student student in ListToArray)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }

技术图片

3. Array转Dictionary

            Dictionary<int, Student> StudentDictionary = studentArray.ToDictionary(key=>key.Id, Studentobj => Studentobj);
            foreach (KeyValuePair<int, Student> student in StudentDictionary)
            {
                Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
            }

技术图片

4. Dictionary转Array

            Student[] DictionaryToArray = StudentDictionary.Values.ToArray();
            foreach (Student student in DictionaryToArray)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }

技术图片

5. List转Dictionary

            Dictionary<int, Student> ListToDictionary = ListToArray.ToDictionary(key => key.Id, value => value);
            foreach (KeyValuePair<int, Student> student in ListToDictionary)
            {
                Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
            }

技术图片

6. Dictionary转List

            List<Student> DictionaryToList = StudentDictionary.Values.ToList();
            foreach (Student student in DictionaryToList)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }

技术图片

以上是关于C# List, Array, Dictionary相互转换的主要内容,如果未能解决你的问题,请参考以下文章

C#一个List或Array集合里是不是有相同数据

在 C# .NET 中,array.Length 与 list.Count 的原因是啥? [复制]

C# 移除数组中重复项

List.Clear() 在 C# 中是如何实现的?

c#如何遍历一个list将值传到数组

C# 排序