本篇文章會向大家執行個體講述以下内容:
- 将數組轉換為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,我先建立了一個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轉換為數組,使用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,使用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 (Student student in DictionaryToArray)
{
Console.WriteLine("Id = "+student.Id+" "+" Name = " +student.Name+" "+" Gender = "+student.Gender);
}
之前已經建立了一個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調用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);
}
希望本文對你有幫助