您好,登錄后才能下訂單哦!
這篇文章主要介紹“C#怎么實現Array,List,Dictionary相互轉換”,在日常操作中,相信很多人在C#怎么實現Array,List,Dictionary相互轉換問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C#怎么實現Array,List,Dictionary相互轉換”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
將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(); }
到此,關于“C#怎么實現Array,List,Dictionary相互轉換”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。