积极答复者
C# dictionary<TKey,TValue>加入键和值后数据类型怎么就变成了KeyValuePair<TKey, TValue>,代码是怎么实现的?

问题
答案
-
因为Enumerate 的时候是使用GetEnumerator函数,而且实现了 IEnumerable<KeyValuePair<TKey, TValue>> 接口
public Dictionary<TKey, TValue>.Enumerator GetEnumerator()
{
return new Dictionary<TKey, TValue>.Enumerator(this, 2);
}而Enumerator的定义是:
public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>, IDisposable, IDictionaryEnumerator, IEnumerator
{
internal const int DictEntry = 1;
internal const int KeyValuePair = 2;
private Dictionary<TKey, TValue> dictionary;
private int version;
private int index;
private KeyValuePair<TKey, TValue> current;
private int getEnumeratorRetType;
/// <summary>Gets the element at the current position of the enumerator.</summary>
/// <returns>The element in the <see cref="T:System.Collections.Generic.Dictionary`2" /> at the current position of the enumerator.</returns>
[__DynamicallyInvokable]
public KeyValuePair<TKey, TValue> Current
{
[__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get
{
return this.current;
}
}.....
当调试器去取值的时候,是使用的Enumerate, 取到的数据类型就是KeyValuePair<TKey, TValue>了。
- 已建议为答案 Barry Wang 2015年8月4日 5:05
- 已标记为答案 齐永刚 2015年8月4日 6:13
全部回复
-
首先:不能直接用对象当成布尔类型用作if判断,应该是使用:
if(discusOne!=null) { …… }
其次:你的 Dictionary的Value类型应该是DateTime类型,不是object类型:
Dictionary<string,DateTime> dic1 = new Dictionary<string,DateTime>();
ASP.NET Forum
Other Discussion Forums
FreeRice Donate
Issues to report
Free Tech Books Search and Download -
首先:不能直接用对象当成布尔类型用作if判断,应该是使用:
if(discusOne!=null) { …… }
其次:你的 Dictionary的Value类型应该是DateTime类型,不是object类型:
Dictionary<string,DateTime> dic1 = new Dictionary<string,DateTime>();
ASP.NET Forum
Other Discussion Forums
FreeRice Donate
Issues to report
Free Tech Books Search and Download -
public bool MoveNext() {
if (version != dictionary.version) {
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
}
// Use unsigned comparison since we set index to dictionary.count+1 when the enumeration ends.
// dictionary.count+1 could be negative if dictionary.count is Int32.MaxValue
while ((uint)index < (uint)dictionary.count) {
if (dictionary.entries[index].hashCode >= 0) {
current = new KeyValuePair<TKey, TValue>(dictionary.entries[index].key, dictionary.entries[index].value);
关键是dictionary<TKey,TValue>在哪里调用的这方法看不到
- 已编辑 齐永刚 2015年8月4日 2:46
-
因为Enumerate 的时候是使用GetEnumerator函数,而且实现了 IEnumerable<KeyValuePair<TKey, TValue>> 接口
public Dictionary<TKey, TValue>.Enumerator GetEnumerator()
{
return new Dictionary<TKey, TValue>.Enumerator(this, 2);
}而Enumerator的定义是:
public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>, IDisposable, IDictionaryEnumerator, IEnumerator
{
internal const int DictEntry = 1;
internal const int KeyValuePair = 2;
private Dictionary<TKey, TValue> dictionary;
private int version;
private int index;
private KeyValuePair<TKey, TValue> current;
private int getEnumeratorRetType;
/// <summary>Gets the element at the current position of the enumerator.</summary>
/// <returns>The element in the <see cref="T:System.Collections.Generic.Dictionary`2" /> at the current position of the enumerator.</returns>
[__DynamicallyInvokable]
public KeyValuePair<TKey, TValue> Current
{
[__DynamicallyInvokable, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get
{
return this.current;
}
}.....
当调试器去取值的时候,是使用的Enumerate, 取到的数据类型就是KeyValuePair<TKey, TValue>了。
- 已建议为答案 Barry Wang 2015年8月4日 5:05
- 已标记为答案 齐永刚 2015年8月4日 6:13