如何正确包装字典< T,U>并公开一个枚举器? [英] How do I correctly wrap a Dictionary<T,U> and expose an enumerator?

查看:135
本文介绍了如何正确包装字典< T,U>并公开一个枚举器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的对象中封装了一个字典。如何公开IEnumerable?



之前

  class HashRunningTotalDB:Dictionary< int,SummaryEntity> 
{
/ ...
}

// WORKS!
static void Main()
{
HashRunningTotalDB tempDB = new HashRunningTotalDB();
// todo:load temp DB

foreach(tempDB中的var项)
{
Console.Writeline(item.Key ++ item.Value。 SomeProperty);
}
}

>

  class HashRunningTotalDB:IEnumerable 
{
Dictionary< int,SummaryEntity> thisHashRunningTotalDB = new Dictionary< int,SummaryEntity>();

//问题:我该如何执行这个GENERIC ENUMERATOR?
//以下内容与以前的实现不同:
IEnumerator IEnumerable.GetEnumerator()
{
return thisHashRunningTotalDB.GetEnumerator();
}


//以下不编译
Dictionary< int,SummaryEntity> .Enumerator IEnumerable< Dictionary< int,SummaryEntity>> .GetEnumerator )
{
return thisHashRunningTotalDB.GetEnumerator();
}
}



static void Main()
{
HashRunningTotalDB tempDB = new HashRunningTotalDB();
// todo:load temp DB


//不工作
foreach(tempDB中的var项)
{
Console.Writeline (item.Key ++ item.Value.SomeProperty);
}
}


解决方案

IEnumerable< KeyValuePair< int,SummaryEntity>>

  class HashRunningTotalDB: IEnumerable< KeyValuePair< int,SummaryEntity>> 
{
字典< int,SummaryEntity> thisHashRunningTotalDB =
new Dictionary< int,SummaryEntity>();

public IEnumerator< KeyValuePair< int,SummaryEntity>> GetEnumerator()
{
return thisHashRunningTotalDB.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}

static void Main()
{
HashRunningTotalDB tempDB = new HashRunningTotalDB();

//现在应该工作
foreach(tempDB中的KeyValuePair< int,SummaryEntity>项)
{
Console.Writeline(item.Key ++ item .Value.SomeProperty);
}
}


I am encapsulating a Dictionary in my object. How do I expose IEnumerable>?

Before

class HashRunningTotalDB : Dictionary<int, SummaryEntity>
{
         /... 
} 

// WORKS!
static void Main ()
{
       HashRunningTotalDB  tempDB = new HashRunningTotalDB();
       //todo: load temp DB

       foreach(var item in tempDB)
       {
           Console.Writeline(item.Key + " " + item.Value.SomeProperty);
       }
}

After

class HashRunningTotalDB : IEnumerable
{
    Dictionary<int, SummaryEntity> thisHashRunningTotalDB = new Dictionary<int, SummaryEntity>();

      //QUESTION:  HOW DO I IMPLEMENT THE GENERIC ENUMERATOR HERE?
    // The following doesn't behave the same as the previous implementation
     IEnumerator IEnumerable.GetEnumerator()
    {
        return thisHashRunningTotalDB.GetEnumerator();
    }


    // The following doesn't compile
     Dictionary<int, SummaryEntity>.Enumerator IEnumerable<Dictionary<int, SummaryEntity>>.GetEnumerator()
    {
        return thisHashRunningTotalDB.GetEnumerator();
    }
} 



static void Main ()
{
       HashRunningTotalDB  tempDB = new HashRunningTotalDB();
       //todo: load temp DB


       // NOT WORKING
       foreach(var item in tempDB)
       {
           Console.Writeline(item.Key + " " + item.Value.SomeProperty);
       }
}

解决方案

Implement IEnumerable<KeyValuePair<int, SummaryEntity>>

class HashRunningTotalDB : IEnumerable<KeyValuePair<int, SummaryEntity>>
{
   Dictionary<int, SummaryEntity> thisHashRunningTotalDB =
      new Dictionary<int, SummaryEntity>();

   public IEnumerator<KeyValuePair<int, SummaryEntity>> GetEnumerator()
   {
      return thisHashRunningTotalDB.GetEnumerator();
   }

   IEnumerator IEnumerable.GetEnumerator()
   {
      return GetEnumerator();
   }
}

static void Main()
{
   HashRunningTotalDB tempDB = new HashRunningTotalDB();

   // should work now
   foreach(KeyValuePair<int, SummaryEntity> item in tempDB)
   {
       Console.Writeline(item.Key + " " + item.Value.SomeProperty);
   }
}

这篇关于如何正确包装字典&lt; T,U&gt;并公开一个枚举器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆