好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

dy("nrwz");

查看更多关于C#数组中List, Dictionary的相互转换问题的详细内容...

  阅读:58次