C#中怎么將一個List轉(zhuǎn)換為只讀的
更新時間:2013年08月19日 09:33:08 作者:
以下是對C#中將一個List轉(zhuǎn)換為只讀的實現(xiàn)方法進行了介紹,需要的朋友可以過來參考下
如題,主要使用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);
復(fù)制代碼 代碼如下:
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);
相關(guān)文章
C#/VB.NET實現(xiàn)在Word中插入或刪除腳注
腳注,是可以附在文章頁面的最底端的,對某些東西加以說明,印在書頁下端的注文。這篇文章將為您展示如何通過C#/VB.NET代碼,以編程方式在Word中插入或刪除腳注,需要的可以參考一下2023-03-03
C#使用yield關(guān)鍵字構(gòu)建迭代器詳解
這篇文章主要為大家詳細(xì)介紹了C#使用yield關(guān)鍵字構(gòu)建迭代器的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
C#微信公眾號開發(fā)之使用MessageHandler簡化消息處理流程
這篇文章介紹了C#微信公眾號開發(fā)之使用MessageHandler簡化消息處理流程,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06

