C# List, Array, Dictionary相互转换
2021-06-06 14:02
标签:name 创建 key 转换 main student inf opp 图片 将数组转换成一个List,我先创建了一个student类型的数组。 C# List, Array, Dictionary相互转换 标签:name 创建 key 转换 main student inf opp 图片 原文地址:https://www.cnblogs.com/LuckyZLi/p/10777066.html
首先,我们定义一个“Student”类,它有三个自动实现属性。 class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
1. Array转换为List
static void Main(string[] args)
{
Student[] studentArray = new Student[3];
studentArray[0] = new Student()
{
Id = 12,
Name = "Lucky",
Gender = "Male"
};
studentArray[1] = new Student()
{
Id = 23,
Name = "Poppy",
Gender = "Male"
};
studentArray[2] = new Student()
{
Id = 55,
Name = "Jack",
Gender = "Female"
};
//foreach循环数组
foreach (Student student in studentArray)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
//1. 数组转成List
List
2. List转换为Array
Student[] ListToArray = StudentList.ToArray
3. Array转Dictionary
Dictionary
4. Dictionary转Array
Student[] DictionaryToArray = StudentDictionary.Values.ToArray();
foreach (Student student in DictionaryToArray)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
5. List转Dictionary
Dictionary
6. Dictionary转List
List
文章标题:C# List, Array, Dictionary相互转换
文章链接:http://soscw.com/index.php/essay/91300.html