本文介绍了通用字典表现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! Hi Group 我正在考虑使用通用词典<>作为一个价值容器内部 我的业务对象,为了跟踪字段的变化或 添加等等。 - 但是在内存使用和性能方面,以极好的 数字(让我们只说100000')实例化/使用Generic Dictionaries有多贵? 有任何实际经验吗? -------------- 简化示例: 公共类SomeItem { public SomeItem() { } private Dictionary< string,object> _values = new Dictionary< string, object>(); private string GetString(string fieldname) { if(_values.ContainsKey(fieldname)) 返回_values [fieldname]; 返回null; } private void SetValue(字符串字段名,对象值) { if(_values.ContainsKey(fieldname)) _values [fieldname] = value; else _values.Add(fieldname,value); } 公共字符串键 { get {return this.GetString(" Key"); } set {this.SetValue(" Key",value); } } 公共字符串ItemName { get {return this.GetString(" ; ITEMNAME"); } set {this.SetValue(" ItemName",value); } } //和更多属性更多 }Hi GroupI was considering using a Generic Dictionary<> as a value container insidemy business objects, for the reason of keeping track of fields changed oradded and so on.- But how expensive is it to instantiate/use Generic Dictionaries in greatnumbers (let''s just say 100000''s ), in terms of memoryuse and performance?Any practical experiences out there?--------------Simplified example:public class SomeItem{public SomeItem(){}private Dictionary<string, object> _values = new Dictionary<string,object>();private string GetString(string fieldname){if(_values.ContainsKey(fieldname))return _values[fieldname];return null;}private void SetValue(string fieldname, object value){if(_values.ContainsKey(fieldname))_values[fieldname] = value;else_values.Add(fieldname, value);}public string Key{get { return this.GetString("Key"); }set { this.SetValue("Key", value); }}public string ItemName{get { return this.GetString("ItemName"); }set { this.SetValue("ItemName", value); }}// and a number of properties more}推荐答案 这里真正的问题不是实例化/使用 Generic Dictionaries有多贵?但是提供我需要的功能的最佳解决方案是什么?一个简单的字符串数组可能是解决方案,取决于您的实际业务需求,或任何包括自定义类的任何内容。您需要做的是比较应用程序的特定需求以确定最小的必要功能集,并询问可以提供最少功能集的工具是什么?在那一点上,多么昂贵并不重要。它是使用该工具;它只是它是最有效的工具。 The real question here is not "how expensive is it to instantiate/use Generic Dictionaries" but "what is the most optimal solution to provide the functionality I require? A simple string array might be the solution, depending on your actual business requirements, or anything up to and including a custom class. What you have to do is compare the specific needs of your app to determine a minimum set of necessary features, and ask what is the tool that can provide that minimum set of features? At that point, it doesn''t matter "how expensive" it is to use that tool; it only matters that it is the most efficient tool for the job. 是的,我明白了你的观点 - 如果是一个字典< string,object>的功能; 正是这里所需要的。 (有点类似于DataRow没有 版本和架构)。 当我用 $查看字典时我只关心性能b $ b反射器。它乍一看似乎是一个相当复杂的结构,有很多拳击和内部的东西正在进行......所以我考虑过解决它 更简单模型,这将需要更多的代码在项目旁边 类 - 但为什么打扰它,如果它实际上表现得非常好...... RYeah, I see your point - the functionality if an Dictionary<string, object>is exactly what is needed here. (somewhat similar to a DataRow withoutversions and schema).I just got concerned with performance when I looked at the Dictionary withreflector. It seemed at first glance like an pretty complex structure, witha lot of boxing and internal stuff going on... so I''ve considered solving itwith a simpler model, that would require a lot more code out side the "item"class- but why bother if it actually performed very well...R 这里真正的问题不是实例化/使用 Generic Dictionaries有多贵。但是提供我需要的功能的最佳解决方案是什么? The real question here is not "how expensive is it to instantiate/use Generic Dictionaries" but "what is the most optimal solution to provide the functionality I require? 我会说它甚至都没有。我会问:什么是最简单,最可靠的b $ b可读和可维护的解决方案,它为我提供了性能和我需要的b $ b功能? 最优解决方案通常远非最简单的解决方案,但是 通常最简单的一个表现得足够*。 当然,这篇文章在解释时才有意义最佳 就性能而言 - 如果你的意思更全面,那么b b b b b b b b b b b b b b b b b b b b感觉,那么我们可能已经同意了:) - Jon Skeet - < sk *** @ pobox.com> http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet 如果回复该组,请不要给我发邮件I would say it''s not even that. I''d ask: "What is the simplest, mostreadable and maintainable solution which gives me the performance andfunctionality I require?"The "most optimal" solution is often far from the simplest one, butoften the simplest one performs well *enough*.Of course, this post only makes sense when interpreting "most optimal"in terms of performance - if you meant it in a more holistic "best inmany different ways" sense, then we probably already agree :)--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeetIf replying to the group, please do not mail me too 这篇关于通用字典表现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 02:49