日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

代码演示 .NET 4.5 自带的 ReadonlyCollection 的使用

發布時間:2023/12/10 asp.net 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 代码演示 .NET 4.5 自带的 ReadonlyCollection 的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

代碼如下:

1.?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace ConfigurationLibrary {public class ConfigElement{public int Id { get; set; }public string Value { get; set; }} }

?

2.

using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks;namespace ConfigurationLibrary {public class ConfigurationContainer{private ReadOnlyDictionary<string, ConfigElement> _configuration;private Dictionary<string, ConfigElement> _mutableConfiguration;public ReadOnlyDictionary<string, ConfigElement> Configuration{get{_configuration =new ReadOnlyDictionary<string, ConfigElement>(_mutableConfiguration);return _configuration;}}public ConfigurationContainer(){_mutableConfiguration = new Dictionary<string, ConfigElement>();_mutableConfiguration.Add("key", new ConfigElement { Id=1, Value="value1"});_mutableConfiguration.Add("key1", new ConfigElement { Id = 1, Value = "value1" });_mutableConfiguration["key2"] = new ConfigElement { Id = 1, Value = "value1" };}public bool AddToConfiguration(string key, ConfigElement value){if (ConfigurationAllowed(key, value)){_mutableConfiguration.Add(key, value);return true;}return false;}private bool ConfigurationAllowed(string key, ConfigElement value){// Put in your checks and balances// here and return the appropriate resultreturn true;}} }

?

3.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ConfigurationLibrary;namespace ConfigurationUser {public class ConfigurationConsumer{IReadOnlyDictionary<string, ConfigurationLibrary.ConfigElement> _config;public ConfigurationConsumer(){_config = new ConfigurationLibrary.ConfigurationContainer().Configuration;}public void DoSomething(){if (_config.ContainsKey("key")){// Do Something Console.WriteLine(string.Format("Did Something, Got - {0}",_config["key"].Value));}else{// Do Something Else.Console.WriteLine("Did Something Else");}}public void BeNaughtyWithConfiguration(){IDictionary<string, ConfigElement> convertToReadWrite= (IDictionary<string, ConfigElement>)_config;ConfigElement element = convertToReadWrite["key"];element.Value = "Haa Haa";Console.WriteLine(element.Value);Console.WriteLine(convertToReadWrite["key"].Value);Console.ReadLine();// 上面的代碼都能運行通過,下面這行代碼將拋出異常。convertToReadWrite.Add("Key12345", new ConfigElement { Id = 100, Value = "Haa Haa" });}} }

?

4.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace ReadOnlyCollectionSample {class Program{static void Main(string[] args){ConfigurationUser.ConfigurationConsumer consumer =new ConfigurationUser.ConfigurationConsumer();consumer.DoSomething();Console.ReadLine();consumer.BeNaughtyWithConfiguration();}} }

?

?

謝謝瀏覽!

轉載于:https://www.cnblogs.com/Music/p/readonly-collection-in-dotnet4_5.html

總結

以上是生活随笔為你收集整理的代码演示 .NET 4.5 自带的 ReadonlyCollection 的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。