相关疑难解决方法(0)

如何使IEnumerable <T>只读?

为什么列表list1Instance和下面代码pMain方法指向同一个集合?

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 …
Run Code Online (Sandbox Code Playgroud)

.net c# generics ienumerable

12
推荐指数
2
解决办法
1万
查看次数

标签 统计

.net ×1

c# ×1

generics ×1

ienumerable ×1