为什么下面代码的Main
方法中的列表list1Instance
和p
指向同一个集合?
class Person
{
public string FirstName = string.Empty;
public string LastName = string.Empty;
public Person(string firstName, string lastName) {
this.FirstName = firstName;
this.LastName = lastName;
}
}
class List1
{
public List<Person> l1 = new List<Person>();
public List1()
{
l1.Add(new Person("f1","l1"));
l1.Add(new Person("f2", "l2"));
l1.Add(new Person("f3", "l3"));
l1.Add(new Person("f4", "l4"));
l1.Add(new Person("f5", "l5"));
}
public IEnumerable<Person> Get()
{
foreach (Person p in l1)
{
yield return p;
}
//return l1.AsReadOnly();
}
}
class Program
{
static void Main(string[] args)
{
List1 list1Instance = new List1();
List<Person> p = new List<Person>(list1Instance.Get());
UpdatePersons(p);
bool sameFirstName = (list1Instance.l1[0].FirstName == p[0].FirstName);
}
private static void UpdatePersons(List<Person> list)
{
list[0].FirstName = "uf1";
}
}
我们能否在不更改 List1.Get()
的返回类型的情况下更改此行为?
谢谢
最佳答案
事实上,IEnumerable<T>
已经是只读的。这意味着您不能用不同的项目替换基础集合中的任何项目。也就是说,您不能更改对 Person
的引用集合中保存的对象。类型Person
但是,它不是只读的,因为它是引用类型(即 class
),您可以通过引用更改其成员。
有两种解决方法:
- 使用
struct
作为返回类型(每次返回时都会复制该值,因此不会更改原始值——顺便说一句,这可能代价高昂) - 在
Person
上使用只读属性键入以完成此任务。
关于c# - 如何使 IEnumerable<T> 只读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/359495/