Tag: 字典

比较在Dictionary中用作Key的对象

我的课: public class myClass { public int A { get; set; } public int B { get; set; } public int C { get; set; } public int D { get; set; } } 和主要的例子: Dictionary<myClass, List> dict = new Dictionary<myClass, List>(); myClass first = new myClass(); first.A = 2; first.B = 3; myClass second […]

C#:Dictionary的索引器返回什么?

当Dictionary中不存在键时, Dictionary的[string]索引器会返回什么? 我是C#的新手,我似乎无法找到像Javadocs一样好的参考。 我得到null ,还是我得到例外?

Dictonary to ToList ArithmeticFlowException

在负载测试期间面临exception,算术导致溢出。 public static IDictionary<string, ISet> SereverConnections = new ConcurrentDictionary<string, ISet>(); public static IList GetUserConnections(string username) { //Key must not be null in any case return null if someone send and empty username if (string.IsNullOrEmpty(username)) return null; ISet connections; Global.SereverConnections.TryGetValue(username, out connections); //this will make the copy of the //return (connections != null ? connections.ToList() ?? […]

在C#中匹配两大字符串

情况如下: 我有一个网页,我已经作为一个字符串刮。 我在MSSQL数据库中有几个字段。 例如,汽车模型,它有一个ID和一个名称,如Mustang或Civic。 它预装了大多数车型。 我想找到我的模型表中任何行的任何匹配项。 因此,如果我在我的模型表中有思域,野马和E350,我想找到我已经刮过的页面中任何三个中的任何一个。 在C#中执行此操作的有效方法是什么? 我正在使用LINQ to SQL与db进行交互。 创建所有模型的字典,标记页面和迭代标记是否有意义? 或者我应该迭代标记并使用WHERE子句并询问数据库是否匹配? //Dictionary dic contains all models from the DB, with the name being the key and the id being the value… foreach(string pageToken in pageTokens) { if(dic.ContainsKey(pageToken)) { //Do what I need to do } } 这两种方法对我来说都很糟糕。 关于我应该做什么的任何建议? 设想交叉的东西,我想可能会很好吗? 这些方法都没有解决当模型名称多于一个单词时发生的情况。如“F150 Extended Cab”。 对此的想法?

Dictionary.Equals()有实现吗?

我有一个字典,我将其与另一个字典进行比较(变量类型为IDictionary)。 执行d1.Equals(d2)会产生错误。 在下面编写自己的代码会产生真实。 两者都是System.Collections.Generic.Dictionary 。 我错过了什么或者Dictionary没有比较键/值的Equals实现吗? private static bool DictEquals(IDictionary d1, IDictionary d2) { if (d1.Count != d2.Count) return false; foreach (KeyValuePair pair in d1) { if (!d2.ContainsKey(pair.Key)) return false; if (!Equals(d2[pair.Key], pair.Value)) return false; } return true; }

C#Serialize Dictionary 为JSON

我试图将Dictionary序列化为JSON,并获得以下exception: new JavaScriptSerializer().Serialize(mydict)` 输入’System.Collections.Generic.Dictionary`2 [[System.UInt64,mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089],[System.Nullable`1 [[System.UInt64,mscorlib,Version =字典的序列化/反序列化不支持2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]],mscorlib,Version = 2.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]]’,键必须是字符串或对象。 是否有捷径可寻? 也许通过LINQ或者相对简洁的东西将ulongs转换为字符串?

使用锁定Dictionary 的键

我有一个Dictionary 。 编辑:有人向我指出,我的榜样很糟糕。 我的全部意图不是更新循环中的引用,而是根据需要更新/获取数据的不同线程更新不同的值。 我将循环更改为方法。 我需要更新我的字典中的项目 – 一次一个键,我想知道在我的Dictionary对象的.key值上使用锁是否有任何问题? private static Dictionary matrixElements = new Dictionary(); //Pseudo-code public static void UpdateValue(string key) { KeyValuePair keyValuePair = matrixElements[key]; lock (keyValuePair.Key) { keyValuePair.Value = SomeMeanMethod(); } } 这会在法庭上举行还是失败? 我只是希望字典中的每个值都被独立锁定,因此锁定(和更新)一个值不会锁定其他值。 此外,我知道锁定将持续很长时间 – 但数据将无效,直到完全更新。

如何将字典内容复制到C#中的新字典?

如何将Dictionary复制到另一个new Dictionary以便它们不是同一个对象?

如何读取C#字符串字典并将其写入文件?

我有一个Dictionary对象,我想写入磁盘并能够从磁盘读取它。 理想情况下,我会避免任何第三方库。 使用常规C#4有一种简单的方法吗? 答案接受了。 摘要: 选项1 – 使用JavaScriptSerializer 优点:不需要第三方图书馆。 此外,使用更现代的格式,即JSON 缺点:难以阅读 – 没有形成。 此外,确实需要引用不太常用的System.Web.Extension程序集,即使应用程序与Web无关。 解: 写: File.WriteAllText(“SomeFile.Txt”, new JavaScriptSerializer().Serialize(dictionary)); 读: var dictionary = new JavaScriptSerializer() .Deserialize<Dictionary>(File.ReadAllText(“SomeFile.txt”)); 选项2 – 使用Linq到Xml 优点:不需要第三方图书馆。 通常不需要添加其他引用。 易读。 缺点: XML不像更现代的JSON那样优先。 写: new XElement(“root”, d.Select(kv => new XElement(kv.Key, kv.Value))) .Save(filename, SaveOptions.OmitDuplicateNamespaces); 读: var dictionary = XElement.Parse(File.ReadAllText(filename)) .Elements() .ToDictionary(k => k.Name.ToString(), v => […]

在自定义类上创建字典样式集合初始值设定项

可能重复: 自定义集合初始化器 我有一个简单的Pair类: public class Pair { public Pair(T1 value1, T2 value2) { Value1 = value1; Value2 = value2; } public T1 Value1 { get; set; } public T2 Value2 { get; set; } } 并且希望能够像Dictionary对象一样定义它,所有内联都像这样: var temp = new Pair[] { {0, “bob”}, {1, “phil”}, {0, “nick”} }; 但它要求我定义一个全新的Pair(0,“bob”)等,我将如何实现它? 像往常一样,谢谢你们!