Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have an Iterator for a Dictionary?

Tags:

c#

I have a Class that uses a dictionary to store some data.

But I want to hide that implementation detail from the outside, to iterate all my data I would like to send an iterator to the ouside of my class

how can I do this?

like image 946
RagnaRock Avatar asked Feb 22 '23 19:02

RagnaRock


2 Answers

public IEnumerable<KeyValuePair<int, string>> Foo()
{
    var implementationDetail = new Dictionary<int, string>
    {
        { 1, "foo" },
        { 2, "bar" },
    };

    return implementationDetail;
}
like image 59
Darin Dimitrov Avatar answered Mar 05 '23 02:03

Darin Dimitrov


You may call the Dictinary.GetEnumerator() that returns an enumerator.

like image 37
KV Prajapati Avatar answered Mar 05 '23 03:03

KV Prajapati