我正在尝试了解Icollection<T>
的 有用性 。我在microsof docs中找到了一些文档:
但是我还是不明白,我自己尝试了这段代码:
ICollection<Person> names = new List<Person>();
names.Add(new Person() { Name= "pedro", Surname ="gutierrez"});
names.Add(new Person() { Name = "Sara", Surname = "Morales" });
foreach (var name in names)
{
Console.WriteLine("Name: {0} , Surname: {1}", name.Name, name.Surname);
}
并尝试了这个:
List<Person> names = new List<Person>();
names.Add(new Person() { Name= "pedro", Surname ="gutierrez"});
names.Add(new Person() { Name = "Sara", Surname = "Morales" });
foreach (var name in names)
{
Console.WriteLine("Name: {0} , Surname: {1}", name.Name, name.Surname);
}
使用List<T>
或Icollection<T>
我总是得到相同的结果。