这篇文章介绍了C#实现Array,List,Dictionary互相转换的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
一、代码实例实现功能
- 将Array转换为List
- 将List转换为Array
- 将Array转换为Dictionary
- 将Dictionary转换为Array
- 将List转换为Dictionary
- 将Dictionary转换为List
二、代码实现
学生类
class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
}
转换实现代码
static void Main(string[] args)
{
#region 创建学生数组
//创建数组
Student[] StudentArray = new Student[3];
//创建创建3个student对象,并赋值给数组的每一个元素
StudentArray[0] = new Student()
{
Id = 0001,
Name = "Tony",
Gender = "M"
};
StudentArray[1] = new Student()
{
Id = 0002,
Name = "Hulk",
Gender = "M"
};
StudentArray[2] = new Student()
{
Id = 0003,
Name = "Black",
Gender = "F"
};
#endregion
Console.WriteLine("=================测试打印信息=================");
//打印Array中学生信息
Console.WriteLine("打印Array中学生信息:");
foreach (Student student in StudentArray)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
//Array转为LIST
List<Student> StudentList = StudentArray.ToList<Student>();
//打印List中的学生信息
Console.WriteLine("打印List中学生信息:");
foreach (Student student in StudentList)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
//LIST转为Array
Student[] ListToArray = StudentList.ToArray<Student>();
Console.WriteLine("打印ListToArray中的学生信息:");
//打印ListToArray中的学生信息
foreach (Student student in ListToArray)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
//Array转换为Dictionary
Dictionary<int, Student> StudentDictionary = StudentArray.ToDictionary(key => key.Id, Studentobj => Studentobj);
//打印ArrayToDictionary中的学生信息
Console.WriteLine("打印ArrayToDictionary中的学生信息:");
foreach (KeyValuePair<int, Student> student in StudentDictionary)
{
Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
}
//Dictionary转换为Array
Student[] DictionaryToArray = StudentDictionary.Values.ToArray();
//打印Dictionary转Array中的学生信息
Console.WriteLine("打印DictionaryToArray中的学生信息:");
foreach (Student student in DictionaryToArray)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
//List转换为Dictionary
Dictionary<int, Student> ListToDictionary = StudentList.ToDictionary(key => key.Id, value => value);
//打印ListToDictionary中的学生信息
Console.WriteLine("打印ListToDictionary中的学生信息:");
foreach (KeyValuePair<int, Student> student in ListToDictionary)
{
Console.WriteLine("Id = " + student.Key + " " + " Name = " + student.Value.Name + " " + " Gender = " + student.Value.Gender);
}
//Dictionary转换为List
List<Student> DictionaryToList = StudentDictionary.Values.ToList();
//打印DictionaryToList中的学生信息
Console.WriteLine("打印DictionaryToList中的学生信息:");
foreach (Student student in DictionaryToList)
{
Console.WriteLine("Id = " + student.Id + " " + " Name = " + student.Name + " " + " Gender = " + student.Gender);
}
Console.WriteLine("===============END===================");
Console.ReadLine();
}
三、结果输出
到此这篇关于C#实现Array,List,Dictionary相互转换的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持得得之家。
沃梦达教程
本文标题为:C#实现Array,List,Dictionary相互转换
基础教程推荐
猜你喜欢
- C#控制台实现飞行棋小游戏 2023-04-22
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- 一个读写csv文件的C#类 2022-11-06
- C# windows语音识别与朗读实例 2023-04-27
- ZooKeeper的安装及部署教程 2023-01-22
- C#类和结构详解 2023-05-30
- C# 调用WebService的方法 2023-03-09
- unity实现动态排行榜 2023-04-27
- winform把Office转成PDF文件 2023-06-14
- C# List实现行转列的通用方案 2022-11-02