Object 循环遍历对象C的ArrayList#
我试图循环遍历类型为(人)的对象的数组列表,因此我创建了两个类: Person.csObject 循环遍历对象C的ArrayList#,object,arraylist,Object,Arraylist,我试图循环遍历类型为(人)的对象的数组列表,因此我创建了两个类: Person.cs using System; namespace GenericTypes { public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } p
using System;
namespace GenericTypes
{
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public DateTime DateTime { get; set; }
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace GenericTypes
{
public class People
{
public ArrayList GetNonGenericPeople()
{
var people = new ArrayList()
{
new Person() {FirstName = "Djerah", LastName = "Ahmed Rafik", Age = 23, DateTime = DateTime.Today},
new Person() {FirstName = "Djerah", LastName = "Amjed Amir", Age = 11, DateTime = DateTime.Today},
new Person() {FirstName = "Gadda", LastName = "Anoir", Age = 25, DateTime = DateTime.Today}
};
return people;
}
public List<Person> GetGenericPeople()
{
var people = new List<Person>()
{
new Person() {FirstName = "Djerah", LastName = "Ahmed Rafik", Age = 23, DateTime = DateTime.Today},
new Person() {FirstName = "Djerah", LastName = "Amjed Amir", Age = 11, DateTime = DateTime.Today},
new Person() {FirstName = "Gadda", LastName = "Anoir", Age = 25, DateTime = DateTime.Today}
};
return people;
}
}
}
namespace GenericTypes
{
class Program
{
static void Main(string[] args)
{
var persons = new People();
var p = persons.GetNonGenericPeople();
foreach (var s in p)
{
Console.WriteLine(s);
}
}
}
}
People.cs
using System;
namespace GenericTypes
{
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public DateTime DateTime { get; set; }
}
}
using System;
using System.Collections;
using System.Collections.Generic;
namespace GenericTypes
{
public class People
{
public ArrayList GetNonGenericPeople()
{
var people = new ArrayList()
{
new Person() {FirstName = "Djerah", LastName = "Ahmed Rafik", Age = 23, DateTime = DateTime.Today},
new Person() {FirstName = "Djerah", LastName = "Amjed Amir", Age = 11, DateTime = DateTime.Today},
new Person() {FirstName = "Gadda", LastName = "Anoir", Age = 25, DateTime = DateTime.Today}
};
return people;
}
public List<Person> GetGenericPeople()
{
var people = new List<Person>()
{
new Person() {FirstName = "Djerah", LastName = "Ahmed Rafik", Age = 23, DateTime = DateTime.Today},
new Person() {FirstName = "Djerah", LastName = "Amjed Amir", Age = 11, DateTime = DateTime.Today},
new Person() {FirstName = "Gadda", LastName = "Anoir", Age = 25, DateTime = DateTime.Today}
};
return people;
}
}
}
namespace GenericTypes
{
class Program
{
static void Main(string[] args)
{
var persons = new People();
var p = persons.GetNonGenericPeople();
foreach (var s in p)
{
Console.WriteLine(s);
}
}
}
}
尝试of type()
Linq,它过滤掉T
实例;另一个选项是Cast()
foreach(类型()第页中的变量s){
...
}
尝试of type()
Linq,它过滤掉T
实例;另一个选项是Cast()
foreach(类型()第页中的变量s){
...
}
@D.AhmedRafik:不客气!但是请记住,ArrayList
是过时的类,List
是集合的更好选择。有一件事我没有弄明白,为什么我们需要将ArrayList强制转换为泛型列表,以便我们可以访问Person的属性ArrayList
不实现IEnumerable
,而只实现IEnumerable
。ArrayList
实例可能包含string
、object
、Form
等项,因此我们要么通过OfType()
过滤掉Person
项,要么通过cast()
转换项。艾哈迈德拉菲克:不客气!但是请记住,ArrayList
是过时的类,List
是集合的更好选择。有一件事我没有弄明白,为什么我们需要将ArrayList强制转换为泛型列表,以便我们可以访问Person的属性ArrayList
不实现IEnumerable
,而只实现IEnumerable
。ArrayList
实例可能包含string
、object
、Form
等项,因此我们必须通过OfType()
过滤出Person
项,或者通过cast()
转换项