idictionary

IDictionary to string

南笙酒味 提交于 2021-02-08 06:39:43
问题 I have created an IDictionary extension to write IDictionary Exception.Data values to a string. The extension code: public static class DictionaryExtension { public static string ToString<TKey, TValue>(this IDictionary<TKey, TValue> source, string keyValueSeparator, string sequenceSeparator) { if (source == null) throw new ArgumentException("Parameter source can not be null."); return source.Aggregate(new StringBuilder(), (sb, x) => sb.Append(x.Key + keyValueSeparator + x.Value +

IDictionary to string

大城市里の小女人 提交于 2021-02-08 06:39:39
问题 I have created an IDictionary extension to write IDictionary Exception.Data values to a string. The extension code: public static class DictionaryExtension { public static string ToString<TKey, TValue>(this IDictionary<TKey, TValue> source, string keyValueSeparator, string sequenceSeparator) { if (source == null) throw new ArgumentException("Parameter source can not be null."); return source.Aggregate(new StringBuilder(), (sb, x) => sb.Append(x.Key + keyValueSeparator + x.Value +

Code Contracts: Ensures Unproven & Requires Unproven

痴心易碎 提交于 2021-02-07 13:19:48
问题 I'm not sure if I'm doing something wrong here or if it needs to be fixed... I have a custom Dictionary wrapper class and here is a snippet of the code that is necessary. public int Count { get { Contract.Ensures(Contract.Result<int>() >= 0); return InternalDictionary.Count; } } public bool ContainsKey(TKey key) { //This contract was suggested by the warning message, if I remove it //I still get the same warning... Contract.Ensures(!Contract.Result<bool>() || Count > 0); return

C# — Need an IDictionary implementation that will allow a null key

孤人 提交于 2020-01-03 07:02:44
问题 Basically, I want something like this: Dictionary<object, string> dict = new Dictionary<object, string>(); dict.Add(null, "Nothing"); dict.Add(1, "One"); Are there any built into the base class library that allow this? The preceding code will throw an exception at runtime when adding the null key. Thanks 回答1: You could avoid using null and create a special singleton value class that does the same thing. For example: public sealed class Nothing { public static readonly Nothing Value = new

C# — Need an IDictionary implementation that will allow a null key

我们两清 提交于 2020-01-03 07:02:08
问题 Basically, I want something like this: Dictionary<object, string> dict = new Dictionary<object, string>(); dict.Add(null, "Nothing"); dict.Add(1, "One"); Are there any built into the base class library that allow this? The preceding code will throw an exception at runtime when adding the null key. Thanks 回答1: You could avoid using null and create a special singleton value class that does the same thing. For example: public sealed class Nothing { public static readonly Nothing Value = new

Allow multiple values for the same key when creating JSON using ExpandoObject and IDictionary

梦想的初衷 提交于 2019-12-24 11:21:40
问题 I am trying to create dynamic JSON using ExpandoObject and IDictionary. During the dynamic creation of JSON there could be instances when the Name or Value would repeat. However when adding the repeated Name or Value to the ExpandoObject, it gives an error: An item with the same key has already been added. Below is my code snippet : DataTable dt_MappedColumns = (DataTable)ViewState["MappedColumns"]; dynamic ManCols = new ExpandoObject(); var dictionary1 = (IDictionary<string, object>)ManCols;

Is there a better data structure than Dictionary if the values are objects and a property of those objects are the keys?

时光怂恿深爱的人放手 提交于 2019-12-19 17:31:53
问题 I have a Dictionary<int, object> where the int is a property of obj . Is there a better data structure for this? I feel like using a property as the key is redundant. This Dictionary<int, obj> is a field in a container class that allows for random indexing into the obj values based on an int id number. The simplified (no exception handling) indexer in the container class would look like: obj this[int id] { get{ return this.myDictionary[id];} } where myDictionary is the aforementioned

Is there a better data structure than Dictionary if the values are objects and a property of those objects are the keys?

别说谁变了你拦得住时间么 提交于 2019-12-19 17:31:14
问题 I have a Dictionary<int, object> where the int is a property of obj . Is there a better data structure for this? I feel like using a property as the key is redundant. This Dictionary<int, obj> is a field in a container class that allows for random indexing into the obj values based on an int id number. The simplified (no exception handling) indexer in the container class would look like: obj this[int id] { get{ return this.myDictionary[id];} } where myDictionary is the aforementioned

Convert List of KeyValuePair into IDictionary “C#”

ぃ、小莉子 提交于 2019-12-18 12:45:33
问题 My scenario, how to convert List<KeyValuePair<string, string>> into IDictionary<string, string> ? 回答1: Very, very simply with LINQ: IDictionary<string, string> dictionary = list.ToDictionary(pair => pair.Key, pair => pair.Value); Note that this will fail if there are any duplicate keys - I assume that's okay? 回答2: Or you can use this extension method to simplify your code: public static class Extensions { public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>( this IEnumerable

最全数据结构详述: List VS IEnumerable VS IQueryable VS ICo

老子叫甜甜 提交于 2019-12-13 11:54:41
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 本文对常用的数据结构详述:Array, ArrayList,List,IList,ICollection, Stack, Queue, HashTable, Dictionary, IQueryable, IEnumerable。 Collection(集合) Collection是数据记录集合, 编写代码过程中,常常需要合适的容器保存临时数据,方便修改和查找,如何选取合适的数据容器,关键在于将执行的数据操作以及数据记录是否大量。 Array(数组) 特征 1. 固定大小,数组的大小是初始化时决定无法修改的数值。 2. 强类型,存储数据元素类型必须在初始化时指定,因此在运行时,不需要耗费额外的时间来定义数组类型,能够大大提升运行效率。 3. 可使用Foreach关键字实现数组迭代和查找。 因为数组大小是固定的,且是强类型数据结构,因此在运行时只占用很少的内存,运行时效率很高。 1: //It is obvious that strArray is 2: //1. string --> Strongly Type 3: //2. Sized=10 --> Fixed Size 4: 5: string [] strArray = new string [10]; 6: 7: for ( int i = 0; i