Icollection <T>和List <T>之间的区别?

时间:2019-06-16 16:48:22

标签: c#

我正在尝试了解Icollection<T> 有用性 。我在microsof docs中找到了一些文档:

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.icollection-1?view=netframework-4.8

但是我还是不明白,我自己尝试了这段代码:

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>我总是得到相同的结果。

0 个答案:

没有答案