欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)Array,List,Dictionary相互轉(zhuǎn)換

 更新時(shí)間:2022年04月23日 15:41:28   作者:農(nóng)碼一生  
這篇文章介紹了C#實(shí)現(xiàn)Array,List,Dictionary互相轉(zhuǎn)換的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、代碼實(shí)例實(shí)現(xiàn)功能

  • 將Array轉(zhuǎn)換為L(zhǎng)ist
  • 將List轉(zhuǎn)換為Array
  • 將Array轉(zhuǎn)換為Dictionary
  • 將Dictionary轉(zhuǎn)換為Array
  • 將List轉(zhuǎn)換為Dictionary
  • 將Dictionary轉(zhuǎn)換為L(zhǎng)ist

二、代碼實(shí)現(xiàn)

 學(xué)生類(lèi)

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

 轉(zhuǎn)換實(shí)現(xiàn)代碼

        static void Main(string[] args)
        {
            #region 創(chuàng)建學(xué)生數(shù)組
            //創(chuàng)建數(shù)組
            Student[] StudentArray = new Student[3];
            //創(chuàng)建創(chuàng)建3個(gè)student對(duì)象,并賦值給數(shù)組的每一個(gè)元素
            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("=================測(cè)試打印信息=================");

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

            //Array轉(zhuǎn)為L(zhǎng)IST
            List<Student> StudentList = StudentArray.ToList<Student>();
            //打印List中的學(xué)生信息
            Console.WriteLine("打印List中學(xué)生信息:");
            foreach (Student student in StudentList)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }

            //LIST轉(zhuǎn)為Array
            Student[] ListToArray = StudentList.ToArray<Student>();
            Console.WriteLine("打印ListToArray中的學(xué)生信息:");
            //打印ListToArray中的學(xué)生信息
            foreach (Student student in ListToArray)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }

            //Array轉(zhuǎn)換為Dictionary
            Dictionary<int, Student> StudentDictionary = StudentArray.ToDictionary(key => key.Id, Studentobj => Studentobj);
            //打印ArrayToDictionary中的學(xué)生信息
            Console.WriteLine("打印ArrayToDictionary中的學(xué)生信息:");
            foreach (KeyValuePair<int, Student> student in StudentDictionary)
            {
                Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
            }

            //Dictionary轉(zhuǎn)換為Array
            Student[] DictionaryToArray = StudentDictionary.Values.ToArray();
            //打印Dictionary轉(zhuǎn)Array中的學(xué)生信息
            Console.WriteLine("打印DictionaryToArray中的學(xué)生信息:");
            foreach (Student student in DictionaryToArray)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }

            //List轉(zhuǎn)換為Dictionary
            Dictionary<int, Student> ListToDictionary = StudentList.ToDictionary(key => key.Id, value => value);
            //打印ListToDictionary中的學(xué)生信息
            Console.WriteLine("打印ListToDictionary中的學(xué)生信息:");
            foreach (KeyValuePair<int, Student> student in ListToDictionary)
            {
                Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
            }

            //Dictionary轉(zhuǎn)換為L(zhǎng)ist
            List<Student> DictionaryToList = StudentDictionary.Values.ToList();
            //打印DictionaryToList中的學(xué)生信息
            Console.WriteLine("打印DictionaryToList中的學(xué)生信息:");
            foreach (Student student in DictionaryToList)
            {
                Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
            }
            Console.WriteLine("===============END===================");
            Console.ReadLine();
        }

三、結(jié)果輸出

到此這篇關(guān)于C#實(shí)現(xiàn)Array,List,Dictionary相互轉(zhuǎn)換的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#排序算法之快速排序

    C#排序算法之快速排序

    下面給出的代碼是以數(shù)組最后一個(gè)元素作為參考元素,這僅是參考元素選取的方式之一。
    2010-09-09
  • 詳解如何在C#中使用COM接口

    詳解如何在C#中使用COM接口

    COM是一種二進(jìn)制接口標(biāo)準(zhǔn),允許軟件組件在同一進(jìn)程中、跨進(jìn)程甚至跨網(wǎng)絡(luò)透明地交互,COM接口定義了一組方法和屬性,這些方法和屬性可以通過(guò)任何支持該模型的語(yǔ)言調(diào)用來(lái)訪問(wèn),本文給大家介紹了如何在C#中使用COM接口,需要的朋友可以參考下
    2025-03-03
  • C# IEnumerable和IEnumerator接口淺析

    C# IEnumerable和IEnumerator接口淺析

    本文主要介紹了C#中IEnumerable和IEnumerator接口的相關(guān)知識(shí),具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02
  • C# WinForm控件對(duì)透明圖片重疊時(shí)出現(xiàn)圖片不透明的簡(jiǎn)單解決方法

    C# WinForm控件對(duì)透明圖片重疊時(shí)出現(xiàn)圖片不透明的簡(jiǎn)單解決方法

    這篇文章主要介紹了C# WinForm控件對(duì)透明圖片重疊時(shí)出現(xiàn)圖片不透明的簡(jiǎn)單解決方法,結(jié)合實(shí)例形式分析了WinForm圖片重疊后造成圖片不透明的原因與相應(yīng)的解決方法,需要的朋友可以參考下
    2016-06-06
  • C#使用ZXing.Net實(shí)現(xiàn)識(shí)別二維碼和條碼

    C#使用ZXing.Net實(shí)現(xiàn)識(shí)別二維碼和條碼

    ZXing用Java實(shí)現(xiàn)的多種格式的一維二維條碼圖像處理庫(kù),而ZXing.Net是其.Net版本的實(shí)現(xiàn),本文主要為大家詳細(xì)介紹了如何使用ZXing.Net實(shí)現(xiàn)識(shí)別二維碼和條碼,需要的可以參考下
    2024-01-01
  • C# 7.0 新特性1之基于Tuple的“多”返回值方法

    C# 7.0 新特性1之基于Tuple的“多”返回值方法

    這篇文章主要為大家詳細(xì)介紹了C# 7.0 新特性1之基于Tuple的“多”返回值方法,感興趣的小伙伴們可以參考一下
    2016-06-06
  • .NET?MemoryCache如何清除全部緩存

    .NET?MemoryCache如何清除全部緩存

    本文主要介紹了.NET?MemoryCache如何清除全部緩存,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • C#使用遠(yuǎn)程服務(wù)調(diào)用框架Apache Thrift

    C#使用遠(yuǎn)程服務(wù)調(diào)用框架Apache Thrift

    這篇文章介紹了C#使用遠(yuǎn)程服務(wù)調(diào)用框架Apache Thrift的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • 關(guān)于C#數(shù)強(qiáng)轉(zhuǎn)會(huì)不會(huì)拋出異常詳解

    關(guān)于C#數(shù)強(qiáng)轉(zhuǎn)會(huì)不會(huì)拋出異常詳解

    這篇文章主要給大家介紹了關(guān)于C#數(shù)強(qiáng)轉(zhuǎn)會(huì)不會(huì)拋出異常的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • 說(shuō)說(shuō)C#的async和await的具體用法

    說(shuō)說(shuō)C#的async和await的具體用法

    本篇文章主要介紹了說(shuō)說(shuō)C#的async和await的具體用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09

最新評(píng)論