C#中怎么將一個(gè)List轉(zhuǎn)換為只讀的
更新時(shí)間:2013年08月19日 09:33:08 作者:
以下是對(duì)C#中將一個(gè)List轉(zhuǎn)換為只讀的實(shí)現(xiàn)方法進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下
如題,主要使用AsReadOnly這個(gè)方法就可以了
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實(shí)現(xiàn)在Word中插入或刪除腳注
腳注,是可以附在文章頁(yè)面的最底端的,對(duì)某些東西加以說(shuō)明,印在書(shū)頁(yè)下端的注文。這篇文章將為您展示如何通過(guò)C#/VB.NET代碼,以編程方式在Word中插入或刪除腳注,需要的可以參考一下2023-03-03
C#使用yield關(guān)鍵字構(gòu)建迭代器詳解
這篇文章主要為大家詳細(xì)介紹了C#使用yield關(guān)鍵字構(gòu)建迭代器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
C#截圖程序類(lèi)似騰訊QQ截圖實(shí)現(xiàn)代碼
拖動(dòng)過(guò)程中顯示當(dāng)前鼠標(biāo)下一小塊的圖像信息 尺寸、顏色信息的 注意 這里顏色是用的ARGB,需要的朋友可以參考下2012-12-12
C#微信公眾號(hào)開(kāi)發(fā)之使用MessageHandler簡(jiǎn)化消息處理流程
這篇文章介紹了C#微信公眾號(hào)開(kāi)發(fā)之使用MessageHandler簡(jiǎn)化消息處理流程,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06

