日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#實現Array,List,Dictionary相互轉換_C#教程

作者:農碼一生 ? 更新時間: 2022-06-21 編程語言

一、代碼實例實現功能

  • 將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();
        }

三、結果輸出

原文鏈接:https://www.cnblogs.com/wml-it/p/15514456.html

欄目分類
最近更新