IList <Person> persons; IList <object> objects;foreach(Person p in persons) { objects.Add(p); }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication18 { public partial class Form1 : Form { public Form1() { InitializeComponent(); IList<Person> persons = new List<Person>(); persons.Add(new Person("1")); persons.Add(new Person("2")); persons.Add(new Person("3")); IList<object> objects = new List<object>(persons.ToArray()); foreach (Person P in objects) MessageBox.Show(P.Name); } class Person { public String Name = String.Empty; public Person(String Name) { this.Name = Name; } } } }
IList <Person> p=new IList <Person>(); foreach(Person p in persons) { }直接赋值
IList <object> objects;foreach(Person p in persons)
{
objects.Add(p);
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication18
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); IList<Person> persons = new List<Person>(); persons.Add(new Person("1"));
persons.Add(new Person("2"));
persons.Add(new Person("3")); IList<object> objects = new List<object>(persons.ToArray()); foreach (Person P in objects)
MessageBox.Show(P.Name);
} class Person
{
public String Name = String.Empty; public Person(String Name)
{
this.Name = Name;
}
}
}
}
{
}直接赋值