what would be the best way to search for a value in a dictionary.
for instance I would like to search for modified objects, would going through the entire collection be the only way to do this?
c#, .net 2.0
class RecA
{
public bool modified {get;set:}
public string txt {get;set;}
}
class RecA_Dic : Dictionary<int,Rec_A>
{
public bool GetItemByKey(int key,out obj)
{
return this.TryGetValue(key, out obj);
}
public List<Rec_A> getModifiedItems()
{
List<Rec_A> li = new List<Rec_A>();
for(int i=0;i<this.count;i++)
if (((Rec_A)this[i]).modified == true)
li.Add((Rec_A)this[i]);
return li;
}
}
Given the constraints (.NET 2.0), you are almost on target.
IEnumerable<Rec_A>plusyield returnis a better alternative, IMO: