gpt4 book ai didi

c# - 如何使 IEnumerable 只读?

转载 作者:可可西里 更新时间:2023-11-01 08:30:54 24 4
gpt4 key购买 nike

为什么下面代码的Main方法中的列表list1Instancep指向同一个集合?

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com