C#實現(xiàn)Array,List,Dictionary相互轉(zhuǎn)換
更新時間:2022年04月23日 15:41:28 作者:農(nóng)碼一生
這篇文章介紹了C#實現(xiàn)Array,List,Dictionary互相轉(zhuǎn)換的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
一、代碼實例實現(xiàn)功能
- 將Array轉(zhuǎn)換為List
- 將List轉(zhuǎn)換為Array
- 將Array轉(zhuǎn)換為Dictionary
- 將Dictionary轉(zhuǎn)換為Array
- 將List轉(zhuǎn)換為Dictionary
- 將Dictionary轉(zhuǎn)換為List
二、代碼實現(xiàn)
學(xué)生類
class Student { public int Id { get; set; } public string Name { get; set; } public string Gender { get; set; } }
轉(zhuǎn)換實現(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個student對象,并賦值給數(shù)組的每一個元素 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中學(xué)生信息 Console.WriteLine("打印Array中學(xué)生信息:"); foreach (Student student in StudentArray) { Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender); } //Array轉(zhuǎn)為LIST 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)換為List 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#實現(xiàn)Array,List,Dictionary相互轉(zhuǎn)換的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# IEnumerable和IEnumerator接口淺析
本文主要介紹了C#中IEnumerable和IEnumerator接口的相關(guān)知識,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02C# WinForm控件對透明圖片重疊時出現(xiàn)圖片不透明的簡單解決方法
這篇文章主要介紹了C# WinForm控件對透明圖片重疊時出現(xiàn)圖片不透明的簡單解決方法,結(jié)合實例形式分析了WinForm圖片重疊后造成圖片不透明的原因與相應(yīng)的解決方法,需要的朋友可以參考下2016-06-06C#使用遠(yuǎn)程服務(wù)調(diào)用框架Apache Thrift
這篇文章介紹了C#使用遠(yuǎn)程服務(wù)調(diào)用框架Apache Thrift的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06關(guān)于C#數(shù)強(qiáng)轉(zhuǎn)會不會拋出異常詳解
這篇文章主要給大家介紹了關(guān)于C#數(shù)強(qiáng)轉(zhuǎn)會不會拋出異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04