C#中怎么将一个List转换为只读的
程序员文章站
2024-02-16 13:24:58
如题,主要使用asreadonly这个方法就可以了复制代码 代码如下:list a = new list {1, 2, 3, 4...
如题,主要使用asreadonly这个方法就可以了
list<int> a = new list<int> {1, 2, 3, 4, 5};
ilist<int> b = a.asreadonly(); // block modification...
ilist<int> c = b.aswritable(); // ... but unblock it again
c.add(6);
debug.assert(a.count == 6); // we've modified the original
ienumerable<int> d = a.select(x => x); // okay, try this...
ilist<int> e = d.aswritable(); // no, can still get round it
e.add(7);
复制代码 代码如下:
list<int> a = new list<int> {1, 2, 3, 4, 5};
ilist<int> b = a.asreadonly(); // block modification...
ilist<int> c = b.aswritable(); // ... but unblock it again
c.add(6);
debug.assert(a.count == 6); // we've modified the original
ienumerable<int> d = a.select(x => x); // okay, try this...
ilist<int> e = d.aswritable(); // no, can still get round it
e.add(7);