C#實(shí)現(xiàn)Array,List,Dictionary相互轉(zhuǎn)換
一、代碼實(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# IEnumerable和IEnumerator接口淺析
本文主要介紹了C#中IEnumerable和IEnumerator接口的相關(guān)知識(shí),具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02C# WinForm控件對(duì)透明圖片重疊時(shí)出現(xiàn)圖片不透明的簡(jiǎn)單解決方法
這篇文章主要介紹了C# WinForm控件對(duì)透明圖片重疊時(shí)出現(xiàn)圖片不透明的簡(jiǎn)單解決方法,結(jié)合實(shí)例形式分析了WinForm圖片重疊后造成圖片不透明的原因與相應(yīng)的解決方法,需要的朋友可以參考下2016-06-06C#使用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-01C#使用遠(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)資料,文中通過(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的具體用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09