我需要组织大约3000个不同的文件,并在游戏过程中的不同时间检索.
我创建了自己的变量结构.我正在考虑在我的应用程序开始时创建一个"Dictionary",并在游戏开始之前简单地加载我的所有文件.
我想知道性能:有这么多条目的字典会导致我的应用程序变慢吗?大字典是否会使"TryGetValue"和"ContainsKey"运行得更慢?
感谢您的建议!
当我SortedDictionary<TK, TV>
在.NET中,我想枚举它,ICollection<KeyValuePair<TK, TV>>
它按预期顺序枚举?
那是KeyValuePair<TK, TV>
用最低的键作为第一个返回,后面KeyValuePair<TK, TV>
跟第二个最低的键等?
注意:只接受通过引用备份的答案.
我的代码中有一个System.Collections.Generic.Dictionary对象,我试图在Visual Studio调试器中的断点处停止时查看其内容..NET中的Dictionary类当然包含键和值的列表.
如果我右键单击加载的对象,并尝试向下钻取其内容,我似乎进入一个无限循环.例如,如果我试图查看包含的键,我会展开Keys元素,它显示一个计数,另一个集合称为"非公共成员".我扩展后者,并获得另一个字典对象,它有一个Keys元素,我可以扩展它以获得另一个"count"和"非公共成员"的实例,我可以扩展,等等:
使用QuickWatch给出了相同的结果,那么如何实际查看对象中包含的键?
我有一个Dictionary<int, object>
在int
是的属性obj
.有没有更好的数据结构?我觉得使用属性是关键是多余的.
这Dictionary<int, obj>
是容器类中的一个字段,允许obj
根据int
id号随机索引值.容器类中的简化(无异常处理)索引器如下所示:
obj this[int id]
{
get{ return this.myDictionary[id];}
}
Run Code Online (Sandbox Code Playgroud)
myDictionary
前面Dictionary<int, obj>
拿着物体在哪里.
这可能是快速随机访问的典型方式,但我想获得第二意见.
我的问题类似于前一个问题,但这个问题的答案不适用于此问题.
好吧,我想为两者IDictionary
和IReadOnlyDictionary
接口编写扩展方法:
public static TValue? GetNullable<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
where TValue : struct
{
return dictionary.ContainsKey(key)
? (TValue?)dictionary[key]
: null;
}
public static TValue? GetNullable<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
where TValue : struct
{
return dictionary.ContainsKey(key)
? (TValue?)dictionary[key]
: null;
}
Run Code Online (Sandbox Code Playgroud)
但是当我将它用于实现两个接口的类(例如Dictionary<Tkey, TValue>
)时,我得到了"模糊的调用".我不想打字var value = myDic.GetNullable<IReadOnlyDictionary<MyKeyType, MyValueType>>(key)
,我希望它只是var value = myDic.GetNullable(key)
.
这可能吗?
我有一个:
var selectedDates = new Dictionary<string, string>();
selectedDates.Add("2014-06-21", DateTime.Now.ToLongDateString());
selectedDates.Add("2014-07-21", DateTime.Now.AddDays(5).ToLongDateString());
selectedDates.Add("2014-08-21", DateTime.Now.AddDays(9).ToLongDateString());
selectedDates.Add("2014-09-21", DateTime.Now.AddDays(14).ToLongDateString());
Run Code Online (Sandbox Code Playgroud)
如何在不知道密钥的情况下循环项目?
例如,我想得到项[0]的值
如果我做:
var item = selectedDates[0].value; // I get an error
Run Code Online (Sandbox Code Playgroud) IDictionary<TKey, TValue>
继承自IEnumerable<KeyValuePair<TKey, TValue>>
,但IDictionary
由于某种原因不继承IEnumerable<DictionaryEntry>
.我想知道为什么?.OfType<DictionaryEntry>()
每当我需要对一个查询进行查询时,我讨厌写这个丑陋的IDictionary
.
我不确定我在这里做错了什么,或者是否需要修复......
我有一个自定义的Dictionary包装类,这里是一段必要的代码片段.
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 InternalDictionary.ContainsKey(key);
}
Run Code Online (Sandbox Code Playgroud)
唯一的原因,我添加了的containsKey行是因为我得到了下面的警告消息(现在仍然如此)Codecontracts: ensures unproven: !Contract.Result<bool>() || @this.Count > 0
.我可以删除这一行并仍然得到相同的问题!
我该怎么做才能摆脱这些问题?
更新:
我也试过(按照建议)......
public Boolean ContainsKey(TKey key)
{
Contract.Requires(Count == 0 || InternalDictionary.ContainsKey(key));
Contract.Ensures(!Contract.Result<bool>() || Count > 0);
return InternalDictionary.ContainsKey(key);
}
Run Code Online (Sandbox Code Playgroud)
警告5方法'My.Collections.Generic.ReadOnlyDictionary …
IDictionary
如果我不知道其中的键和值的具体类型,我将如何遍历键和值的值,因此只想将它们视为object
s?
如果我这样做:
foreach(var x in myIDictionary) { ... }
我认为x
是一个object
.但我怎么会得到Key
与Value
出它(这两个类型为object
S),在指定的IDictionaryEnumerator
?有IKeyValuePair
没有通用参数吗?
我想我可以手动循环使用枚举器MoveNext
等,但我觉得必须有办法做到这一点foreach
!
我想知道是否可以IDictionary
通过其键删除项目,同时获取已删除的实际值?
就像是:
Dictionary<string,string> myDic = new Dictionary<string,string>();
myDic["key1"] = "value1";
string removed;
if (nameValues.Remove("key1", out removed)) //No overload for this...
{
Console.WriteLine($"We have just remove {removed}");
}
Run Code Online (Sandbox Code Playgroud)
//We have just remove value1
Run Code Online (Sandbox Code Playgroud) idictionary ×10
c# ×9
.net ×2
dictionary ×2
ienumerable ×2
generics ×1
performance ×1
silverlight ×1