国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問(wèn)題

2019-10-29 21:16:18
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本篇文章會(huì)向大家實(shí)例講述以下內(nèi)容:

  • 將數(shù)組轉(zhuǎn)換為L(zhǎng)ist
  • 將List轉(zhuǎn)換為數(shù)組
  • 將數(shù)組轉(zhuǎn)換為Dictionary
  • 將Dictionary 轉(zhuǎn)換為數(shù)組
  • 將List轉(zhuǎn)換為Dictionary
  • 將Dictionary轉(zhuǎn)換為L(zhǎng)ist

首先這里定義了一個(gè)“Student”的類,它有三個(gè)自動(dòng)實(shí)現(xiàn)屬性。

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

將數(shù)組轉(zhuǎn)換為L(zhǎng)ist

將數(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對(duì)象,并賦值給數(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"  };

接下來(lái),使用foreach遍歷這個(gè)數(shù)組。

foreach (Student student in StudentArray) { Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender); }

運(yùn)行程序

c#,數(shù)組,list,dictionary,轉(zhuǎn)換

接下來(lái)將這個(gè)數(shù)組轉(zhuǎn)換為L(zhǎng)ist,我們添加System.Linq命名空間,然后調(diào)用ToList()擴(kuò)展方法。這里我們就調(diào)用StudentArray.ToList()

注意這個(gè)ToList方法的返回類型,它返回的是List< Student >對(duì)象,這說(shuō)明我們可以創(chuàng)建一個(gè)該類型的對(duì)象來(lái)保存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)行程序

c#,數(shù)組,list,dictionary,轉(zhuǎ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)行程序

#a666b152753b4fb6c003e1eb5ba6cefc#

將數(shù)組轉(zhuǎn)換為Dictionary

將數(shù)組轉(zhuǎn)換成Dictionary,使用ToDictionary()擴(kuò)展方法。這里就可以用StudentArray.ToDictonary(

看這個(gè)方法需要的參數(shù),第一個(gè)參數(shù)需要鍵和第二個(gè)參數(shù)需要值。我們知道Dictionary是一個(gè)泛型,它是鍵/值對(duì)類型的集合。因此,這里我們用一個(gè)lambda表達(dá)式傳遞Dictionary對(duì)象名稱。

StudentArray.ToDictionary(key => key.Id,Studentobj => Studentobj);

這個(gè)ToDictionary方法返回的類型是Dictionary 對(duì)象。 其鍵/值對(duì)<int,Student>類型,同樣說(shuō)明我們可以創(chuàng)建一個(gè)該類型的對(duì)象來(lái)存儲(chǔ)ToDictionary方法得到的數(shù)據(jù)。

Dictionary<int, Student> StudentDictionary = StudentArray.ToDictionary(key => key.Id,Studentobj => Studentobj);

使用foreach從這個(gè)StudentDictionary對(duì)象遍歷學(xué)生資料,如下:

foreach (KeyValuePair<int, Student> student in StudentDictionary){ Console.WriteLine("Id = "+student.Key+" "+" Name = "+student.Value.Name+" "+" Gender = "+student.Value.Gender);}

運(yùn)行程序

c#,數(shù)組,list,dictionary,轉(zhuǎn)換

將Dictionary轉(zhuǎn)換為數(shù)組

將Dictionary轉(zhuǎn)換成數(shù)組,使用ToArray擴(kuò)展方法。在之前,需要獲取Dictionary對(duì)象的集合中的值,所以我們使用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)行程序

c#,數(shù)組,list,dictionary,轉(zhuǎn)換

將List轉(zhuǎn)換為Dictionary

之前已經(jīng)創(chuàng)建了一個(gè)StudentList學(xué)生對(duì)象,將StudentList轉(zhuǎn)換為Dictionary我們調(diào)用ToDictionary方法。

Dictionary<int, Student> ListToDictionary = StudentList.ToDictionary(key => key.Id, value => value);

對(duì)于ToDictionary方法的兩個(gè)參數(shù),我們分別通過(guò)鍵和值傳遞其對(duì)象。這里ToDictionary被賦值,并返回了一個(gè)< int,Student >Dictionary 對(duì)象。所以我們創(chuàng)建該類型的對(duì)象然后存儲(chǔ)返回的數(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)行程序

c#,數(shù)組,list,dictionary,轉(zhuǎn)換

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

將Dictionary 轉(zhuǎn)換成List調(diào)用ToList方法,之前已經(jīng)創(chuàng)建了一個(gè)StudentDictionary對(duì)象。直接看如何這個(gè)對(duì)象轉(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)行程序

c#,數(shù)組,list,dictionary,轉(zhuǎn)換

以上所述是小編給大家介紹的#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到c#教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 沽源县| 麻城市| 屯留县| 彩票| 天津市| 肃北| 巴东县| 勃利县| 日土县| 大丰市| 三门峡市| 沙河市| 钟祥市| 萨迦县| 措勤县| 河源市| 通江县| 宁远县| 十堰市| 房山区| 大英县| 洞头县| 错那县| 定西市| 吉水县| 达孜县| 襄汾县| 广昌县| 镇赉县| 高密市| 蓝山县| 乐平市| 当阳市| 洪湖市| 通许县| 平山县| 贺州市| 江城| 双鸭山市| 泸定县| 布尔津县|