欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年12月28日 15:07:22   投稿:mrr  
這篇文章主要介紹了C#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問題,本文給大家介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下

本篇文章會向大家實例講述以下內(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

首先這里定義了一個“Student”的類,它有三個自動實現(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)換成一個List,我先創(chuàng)建了一個student類型的數(shù)組。

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

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

運行程序

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

注意這個ToList方法的返回類型,它返回的是List< Student >對象,這說明我們可以創(chuàng)建一個該類型的對象來保存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);
 }

運行程序

將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);
}

運行程序

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

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

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

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

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

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

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

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

運行程序

將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);
}

運行程序

將List轉(zhuǎn)換為Dictionary

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

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

對于ToDictionary方法的兩個參數(shù),我們分別通過鍵和值傳遞其對象。這里ToDictionary被賦值,并返回了一個< 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);
}

運行程序

將Dictionary轉(zhuǎn)換為List

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

運行程序

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

相關(guān)文章

  • C#解決多IfElse判斷語句和Switch語句問題的方法分享

    C#解決多IfElse判斷語句和Switch語句問題的方法分享

    這篇文章主要為大家介紹C#如何使用設(shè)計模式中的策略模式和委托來解決多個IfElse判斷語句和Switch語句,這種替換方式在其他語言也一樣可以做到,感興趣的可以了解一下
    2022-12-12
  • C#中WPF內(nèi)存回收與釋放LierdaCracker的實現(xiàn)

    C#中WPF內(nèi)存回收與釋放LierdaCracker的實現(xiàn)

    本文主要介紹了C#中WPF內(nèi)存回收與釋放LierdaCracker的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • C#泛型設(shè)計需要注意的一個小陷阱

    C#泛型設(shè)計需要注意的一個小陷阱

    這篇文章主要給大家介紹了關(guān)于C#泛型設(shè)計需要注意的一個小陷阱,文中通過示例代碼以及圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • C#多線程系列之原子操作

    C#多線程系列之原子操作

    本文詳細(xì)講解了C#多線程的原子操作,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-02-02
  • Unity實戰(zhàn)之制作動畫編輯器

    Unity實戰(zhàn)之制作動畫編輯器

    為了更方便地為UI視圖添加動畫,將動畫的編輯功能封裝在了UI View類中,可以通過編輯器快速的為視圖編輯動畫。本文將通過Unity制作一個動畫編輯器,需要的可以參考一下
    2022-02-02
  • C#使用EF連接PGSql數(shù)據(jù)庫的完整步驟

    C#使用EF連接PGSql數(shù)據(jù)庫的完整步驟

    這篇文章主要給大家介紹了關(guān)于C#使用EF連接PGSql數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • C#動態(tài)生成PictureBox并指定圖片的方法

    C#動態(tài)生成PictureBox并指定圖片的方法

    這篇文章主要介紹了C#動態(tài)生成PictureBox并指定圖片的方法,實例分析了C#圖形控件的動態(tài)生成及使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • c# 數(shù)據(jù)類型占用的字節(jié)數(shù)介紹

    c# 數(shù)據(jù)類型占用的字節(jié)數(shù)介紹

    本篇文章主要是對c#中數(shù)據(jù)類型占用的字節(jié)數(shù)進(jìn)行了詳細(xì)的介紹。需要的朋友可以過來參考下,希望對大家有所幫助
    2014-01-01
  • C#事件實例詳解

    C#事件實例詳解

    這篇文章主要介紹了C#事件實例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • c# 如何實現(xiàn)一個簡單的json解析器

    c# 如何實現(xiàn)一個簡單的json解析器

    這篇文章主要介紹了c# 如何實現(xiàn)一個簡單的json解析器,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07

最新評論