Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 下面的方法是返回安全列表枚举器的好方法吗? 公共类Foo{ 公共字符串Prop1{get;set;} 公共字符串Prop2{get;set;} 公共食品(食品来源){ this.Prop1=source.Prop1; this.Prop2=source.Prop2; } } 公共班机 { 私有列表项=新列表(); 公共IEnumerable GetItems(){ foreach(Foo-Foo-in-items){ 收益-收益-新Foo(Foo); } } }_C# - Fatal编程技术网

C# 下面的方法是返回安全列表枚举器的好方法吗? 公共类Foo{ 公共字符串Prop1{get;set;} 公共字符串Prop2{get;set;} 公共食品(食品来源){ this.Prop1=source.Prop1; this.Prop2=source.Prop2; } } 公共班机 { 私有列表项=新列表(); 公共IEnumerable GetItems(){ foreach(Foo-Foo-in-items){ 收益-收益-新Foo(Foo); } } }

C# 下面的方法是返回安全列表枚举器的好方法吗? 公共类Foo{ 公共字符串Prop1{get;set;} 公共字符串Prop2{get;set;} 公共食品(食品来源){ this.Prop1=source.Prop1; this.Prop2=source.Prop2; } } 公共班机 { 私有列表项=新列表(); 公共IEnumerable GetItems(){ foreach(Foo-Foo-in-items){ 收益-收益-新Foo(Foo); } } },c#,C#,这取决于你所说的安全。如果调用对象只是从foo读取信息。而不是: public class Foo{ public string Prop1 {get;set;} public string Prop2 {get;set;} public Foo(Foo source) { this.Prop1 = source.Prop1; this.Prop2 = source.Prop2;

这取决于你所说的安全。如果调用对象只是从foo读取信息。而不是:

 public class Foo{
        public string Prop1 {get;set;}
        public string Prop2 {get;set;}

        public Foo(Foo source) {
            this.Prop1 = source.Prop1;
            this.Prop2 = source.Prop2;
        }
    }

    public class Main
    {
        private List<Foo> items = new List<Foo>();

        public IEnumerable<Foo> GetItems() {
            foreach (Foo foo in items) {
                yield return new Foo(foo);
            }
        }
    }
public IEnumerable GetItems(){
foreach(Foo-Foo-in-items){
收益-收益-新Foo(Foo);
}
为什么不干脆做:

public IEnumerable<Foo> GetItems() {
            foreach (Foo foo in items) {
                yield return new Foo(foo);
            }
public IEnumerable GetItems(){
foreach(Foo-Foo-in-items){
收益率;
}
如果您试图阻止调用对象修改原始的foo,那么您所做的将起作用

您还可以将foo设置为不可变,并且不必担心是否正确复制了所有属性等。这样,您就不必每次通过枚举器返回新对象时都创建新对象

eric lippert关于不变性的帖子:

关于不变性的其他链接:

免责声明:我懂Java,但对C语言几乎没有经验#

如果您试图创建一个副本列表,以使原始列表元素保持“不可变”,并希望使其尽可能通用,那么您可能应该研究克隆,或者更准确地说


否则,仅创建项foo的新实例并不一定能确保您拥有一个完全初始化的副本?

好的,因此看起来您正在寻找一个不可变的迭代器。这是可行的,但效率不高。您可以尝试将您的类设置为:

public IEnumerable<Foo> GetItems() {
            foreach (Foo foo in items) {
                yield return foo;
            }

它不是完全不可变的,但大多数情况下是不可变的。

源不能修改Prop1吗?现在调用方有一个修改过的Prop1,却没有意识到它?@user200295-字符串是不可变的。如果属性是类或其他可变对象呢?@user200295-我只是吹毛求疵,但字符串是类。
public class Foo {
    public string Prop1 { get; private set;}
    public string Prop2 { get; private set;}

    public Foo(string prop1, string prop2) {
        this.Prop1 = prop1;
        this.Prop2 = prop2;
    }
}