Skip to main content
 首页 » 编程设计

c#之如何在嵌套字典上实现 IEnumerableT>

2024年02月05日13cyq1162

我有这门课:

public class MyClass<T> : IEnumerable<T> 
{ 
    private Dictionary<T, Dictionary<T, double>> data = new Dictionary<T, Dictionary<T, double>>(); 
 
    public IEnumerator<T> GetEnumerator() 
    { 
        //Return what??? 
    } 
 
    IEnumerator IEnumerable.GetEnumerator() 
    { 
        return this.GetEnumerator(); 
    } 
} 

但是如何实现 GetEnumerator?

请您参考如下方法:

字典有 Keys 类型 T 的集合:

public IEnumerator<T> GetEnumerator() 
{ 
    return data.Keys.GetEnumerator(); 
}