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

首頁 > 編程 > C# > 正文

C#數組中List, Dictionary的相互轉換問題

2020-01-24 00:51:40
字體:
來源:轉載
供稿:網友

本篇文章會向大家實例講述以下內容:

  • 將數組轉換為List
  • 將List轉換為數組
  • 將數組轉換為Dictionary
  • 將Dictionary 轉換為數組
  • 將List轉換為Dictionary
  • 將Dictionary轉換為List

首先這里定義了一個“Student”的類,它有三個自動實現屬性。

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

將數組轉換為List

將數組轉換成一個List,我先創建了一個student類型的數組。

static void Main (string[] args)  {  //創建數組  Student[] StudentArray = new Student[3];  //創建創建3個student對象,并賦值給數組的每一個元素  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遍歷這個數組。

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

運行程序

接下來將這個數組轉換為List,我們添加System.Linq命名空間,然后調用ToList()擴展方法。這里我們就調用StudentArray.ToList()

注意這個ToList方法的返回類型,它返回的是List< Student >對象,這說明我們可以創建一個該類型的對象來保存ToList方法返回的數據。

List<Student> StudentList = StudentArray.ToList<Student>();

使用foreach從StudentList中獲取所有的學生資料。

List<Student> StudentList = StudentArray.ToList<Student>();foreach (Student student in StudentList) { Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender); }

運行程序

將List轉換為數組

將List轉換為數組,使用System.Linq命名空間下的ToArray()擴展方法。

Student[] ListToArray = StudentList.ToArray<Student>();

使用foreach遍歷學生資料

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

運行程序

將數組轉換為Dictionary

將數組轉換成Dictionary,使用ToDictionary()擴展方法。這里就可以用StudentArray.ToDictonary(

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

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

這個ToDictionary方法返回的類型是Dictionary 對象。 其鍵/值對<int,Student>類型,同樣說明我們可以創建一個該類型的對象來存儲ToDictionary方法得到的數據。

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

使用foreach從這個StudentDictionary對象遍歷學生資料,如下:

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

運行程序

將Dictionary轉換為數組

將Dictionary轉換成數組,使用ToArray擴展方法。在之前,需要獲取Dictionary對象的集合中的值,所以我們使用Values屬性的ToArray方法。

Student[] DictionaryToArray = StudentDictionary.Values.ToArray();

使用foreach遍歷學生資料

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

運行程序

將List轉換為Dictionary

之前已經創建了一個StudentList學生對象,將StudentList轉換為Dictionary我們調用ToDictionary方法。

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

對于ToDictionary方法的兩個參數,我們分別通過鍵和值傳遞其對象。這里ToDictionary被賦值,并返回了一個< int,Student >Dictionary 對象。所以我們創建該類型的對象然后存儲返回的數據,最后用foreach獲取學生資料。

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

運行程序

將Dictionary轉換為List

將Dictionary 轉換成List調用ToList方法,之前已經創建了一個StudentDictionary對象。直接看如何這個對象轉換到list.

List<Student> DictionaryToList = StudentDictionary.Values.ToList();foreach (Student student in DictionaryToList){ Console.WriteLine("Id = "+student.Id+" "+" Name = "+student.Name+" "+" Gender = "+student.Gender);}

運行程序

以上所述是小編給大家介紹的#數組中List, Dictionary的相互轉換問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大竹县| 肃南| 阿尔山市| 建昌县| 玉树县| 安阳市| 历史| 云霄县| 磴口县| 桃江县| 墨玉县| 柳林县| 大田县| 蕲春县| 岳阳县| 鄂托克旗| 利津县| 广昌县| 万全县| 紫金县| 绍兴县| 江津市| 额敏县| 长乐市| 边坝县| 元谋县| 瑞丽市| 蒲城县| 电白县| 安塞县| 汉中市| 聂荣县| 洛浦县| 班戈县| 怀化市| 阿拉善右旗| 海门市| 江川县| 集安市| 进贤县| 桓台县|