C#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問題
本篇文章會向大家實(shí)例講述以下內(nèi)容:
- 將數(shù)組轉(zhuǎn)換為List
- 將List轉(zhuǎn)換為數(shù)組
- 將數(shù)組轉(zhuǎn)換為Dictionary
- 將Dictionary 轉(zhuǎn)換為數(shù)組
- 將List轉(zhuǎn)換為Dictionary
- 將Dictionary轉(zhuǎn)換為List
首先這里定義了一個(gè)“Student”的類,它有三個(gè)自動實(shí)現(xiàn)屬性。
class Student { public int Id { get; set; } public string Name { get; set; } public string Gender { get; set; } }
將數(shù)組轉(zhuǎn)換為List
將數(shù)組轉(zhuǎn)換成一個(gè)List,我先創(chuàng)建了一個(gè)student類型的數(shù)組。
static void Main (string[] args) { //創(chuàng)建數(shù)組 Student[] StudentArray = new Student[3]; //創(chuàng)建創(chuàng)建3個(gè)student對象,并賦值給數(shù)組的每一個(gè)元素 StudentArray[0] = new Student() { Id = 203, Name ="Tony Stark", Gender ="Male" }; StudentArray[1] = new Student() { Id = 205, Name="Hulk", Gender = "Male" }; StudentArray[2] = new Student() { Id = 210, Name ="Black Widow", Gender="Female" };
接下來,使用foreach遍歷這個(gè)數(shù)組。
foreach (Student student in StudentArray) { Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender); }
運(yùn)行程序
接下來將這個(gè)數(shù)組轉(zhuǎn)換為List,我們添加System.Linq命名空間,然后調(diào)用ToList()擴(kuò)展方法。這里我們就調(diào)用StudentArray.ToList()
注意這個(gè)ToList方法的返回類型,它返回的是List< Student >對象,這說明我們可以創(chuàng)建一個(gè)該類型的對象來保存ToList方法返回的數(shù)據(jù)。
List<Student> StudentList = StudentArray.ToList<Student>();
使用foreach從StudentList中獲取所有的學(xué)生資料。
List<Student> StudentList = StudentArray.ToList<Student>(); foreach (Student student in StudentList) { Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender); }
運(yùn)行程序
將List轉(zhuǎn)換為數(shù)組
將List轉(zhuǎn)換為數(shù)組,使用System.Linq命名空間下的ToArray()擴(kuò)展方法。
Student[] ListToArray = StudentList.ToArray<Student>();
使用foreach遍歷學(xué)生資料
foreach (Student student in ListToArray) { Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender); }
運(yùn)行程序
將數(shù)組轉(zhuǎn)換為Dictionary
將數(shù)組轉(zhuǎn)換成Dictionary,使用ToDictionary()擴(kuò)展方法。這里就可以用StudentArray.ToDictonary(
看這個(gè)方法需要的參數(shù),第一個(gè)參數(shù)需要鍵和第二個(gè)參數(shù)需要值。我們知道Dictionary是一個(gè)泛型,它是鍵/值對類型的集合。因此,這里我們用一個(gè)lambda表達(dá)式傳遞Dictionary對象名稱。
StudentArray.ToDictionary(key => key.Id,Studentobj => Studentobj);
這個(gè)ToDictionary方法返回的類型是Dictionary 對象。 其鍵/值對<int,Student>類型,同樣說明我們可以創(chuàng)建一個(gè)該類型的對象來存儲ToDictionary方法得到的數(shù)據(jù)。
Dictionary<int, Student> StudentDictionary = StudentArray.ToDictionary(key => key.Id,Studentobj => Studentobj);
使用foreach從這個(gè)StudentDictionary對象遍歷學(xué)生資料,如下:
foreach (KeyValuePair<int, Student> student in StudentDictionary) { Console.WriteLine("Id = "+student.Key+" "+" Name = "+student.Value.Name+" "+" Gender = "+student.Value.Gender); }
運(yùn)行程序
將Dictionary轉(zhuǎn)換為數(shù)組
將Dictionary轉(zhuǎn)換成數(shù)組,使用ToArray擴(kuò)展方法。在之前,需要獲取Dictionary對象的集合中的值,所以我們使用Values屬性的ToArray方法。
Student[] DictionaryToArray = StudentDictionary.Values.ToArray();
使用foreach遍歷學(xué)生資料
foreach (Student student in DictionaryToArray) { Console.WriteLine("Id = "+student.Id+" "+" Name = " +student.Name+" "+" Gender = "+student.Gender); }
運(yùn)行程序
將List轉(zhuǎn)換為Dictionary
之前已經(jīng)創(chuàng)建了一個(gè)StudentList學(xué)生對象,將StudentList轉(zhuǎn)換為Dictionary我們調(diào)用ToDictionary方法。
Dictionary<int, Student> ListToDictionary = StudentList.ToDictionary(key => key.Id, value => value);
對于ToDictionary方法的兩個(gè)參數(shù),我們分別通過鍵和值傳遞其對象。這里ToDictionary被賦值,并返回了一個(gè)< int,Student >Dictionary 對象。所以我們創(chuàng)建該類型的對象然后存儲返回的數(shù)據(jù),最后用foreach獲取學(xué)生資料。
foreach (KeyValuePair<int,Student> student in ListToDictionary) { Console.WriteLine("Id = "+student.Key+" "+" Name = " +student.Value.Name+" "+" Gender = "+student.Value.Gender); }
運(yùn)行程序
將Dictionary轉(zhuǎn)換為List
將Dictionary 轉(zhuǎn)換成List調(diào)用ToList方法,之前已經(jīng)創(chuàng)建了一個(gè)StudentDictionary對象。直接看如何這個(gè)對象轉(zhuǎn)換到list.
List<Student> DictionaryToList = StudentDictionary.Values.ToList(); foreach (Student student in DictionaryToList) { Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender); }
運(yùn)行程序
以上所述是小編給大家介紹的#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
上一篇:C#實(shí)現(xiàn)JSON字符串序列化與反序列化的方法
欄 目:C#教程
下一篇:C# 表達(dá)式樹Expression Trees的知識梳理
本文標(biāo)題:C#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問題
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6048.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10Extjs4如何處理后臺json數(shù)據(jù)中日期和時(shí)間
- 01-10C#中DataGridView常用操作實(shí)例小結(jié)
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10C#利用反射技術(shù)實(shí)現(xiàn)去掉按鈕選中時(shí)的邊框效果
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#中查找Dictionary中的重復(fù)值的方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery