C#数组中List, Dictionary的相互转换问题

 更新时间:2016年12月28日 15:07:22   投稿:mrr  
这篇文章主要介绍了C#数组中List, Dictionary的相互转换问题,本文给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下

本篇文章会向大家实例讲述以下内容:

  • 将数组转换为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的相互转换问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • C#解决多IfElse判断语句和Switch语句问题的方法分享

    C#解决多IfElse判断语句和Switch语句问题的方法分享

    这篇文章主要为大家介绍C#如何使用设计模式中的策略模式和委托来解决多个IfElse判断语句和Switch语句,这种替换方式在其他语言也一样可以做到,感兴趣的可以了解一下
    2022-12-12
  • C#中WPF内存回收与释放LierdaCracker的实现

    C#中WPF内存回收与释放LierdaCracker的实现

    本文主要介绍了C#中WPF内存回收与释放LierdaCracker的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • C#泛型设计需要注意的一个小陷阱

    C#泛型设计需要注意的一个小陷阱

    这篇文章主要给大家介绍了关于C#泛型设计需要注意的一个小陷阱,文中通过示例代码以及图文介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • C#多线程系列之原子操作

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

    本文详细讲解了C#多线程的原子操作,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • Unity实战之制作动画编辑器

    Unity实战之制作动画编辑器

    为了更方便地为UI视图添加动画,将动画的编辑功能封装在了UI View类中,可以通过编辑器快速的为视图编辑动画。本文将通过Unity制作一个动画编辑器,需要的可以参考一下
    2022-02-02
  • C#使用EF连接PGSql数据库的完整步骤

    C#使用EF连接PGSql数据库的完整步骤

    这篇文章主要给大家介绍了关于C#使用EF连接PGSql数据库的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
    2019-01-01
  • C#动态生成PictureBox并指定图片的方法

    C#动态生成PictureBox并指定图片的方法

    这篇文章主要介绍了C#动态生成PictureBox并指定图片的方法,实例分析了C#图形控件的动态生成及使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • c# 数据类型占用的字节数介绍

    c# 数据类型占用的字节数介绍

    本篇文章主要是对c#中数据类型占用的字节数进行了详细的介绍。需要的朋友可以过来参考下,希望对大家有所帮助
    2014-01-01
  • C#事件实例详解

    C#事件实例详解

    这篇文章主要介绍了C#事件实例详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • c# 如何实现一个简单的json解析器

    c# 如何实现一个简单的json解析器

    这篇文章主要介绍了c# 如何实现一个简单的json解析器,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07

最新评论