Search code examples
c#default

How to check if dictionary's enumerator is null?


I am using c# code and curious about checking if Enumerator structure is empty or not.

What I guess is like this.

Dictionary<key, value> dicItems = new Dictionary<key, value>();

Dictionary<key, value>.Enumerator handyEnumerator = dicItems.GetEnumerator();

void UpdateEveryFrame()
{
    if(handyEnumerator == default(????))  // do something..
}

In fact, I will do by counting dictionary's count.

But I totally want to know how to set Enumerator of dictionary to default(type).

Anyone?


Solution

  • It looks like you're trying to create an enumerator that you can re-use in UpdateEveryFrame. There's really no need to do that. You're far better off creating a new enumerator when you need to enumerate through the collection.

    Dictionary<key, value> dicItems = new Dictionary<key, value>();
    
    void UpdateEveryFrame()
    {
        foreach (var item in dicItems) 
        {
            // Do something to the item.
        }
    }