An open API service indexing awesome lists of open source software.

https://github.com/somdipdey/performance-difference-between-different-ways-of-iterating-over-a-dictionary-in-csharp

Performance Difference Between Different Ways Of Iterating Over A Dictionary in C#
https://github.com/somdipdey/performance-difference-between-different-ways-of-iterating-over-a-dictionary-in-csharp

csharp csharp-code dictionary dictionary-variable iterables iterator key-value

Last synced: 6 months ago
JSON representation

Performance Difference Between Different Ways Of Iterating Over A Dictionary in C#

Awesome Lists containing this project

README

          

# Performance-Difference-Between-Different-Ways-Of-Iterating-Over-A-Dictionary-In-CSharp (C#)

This program evaluates the performance of three different ways of iterating over a Dictionary in C#.

# The Three Different Methods of Iterating Over Dictionary
First, let's assume that the Dictionary variable is as follows:

C#
Dictionary profilingDictionary;

## Method1
Using old-school for loop as follows:

C#

for (int i = 0; i < profilingDictionary.Count; i++ )
{
int key = i;
string value = profilingDictionary[i];
// Do something here
}
## Method 2
Using foreach lazy coding as follows:

C#

foreach (var entry in profilingDictionary)
{
int key = entry.Key;
string value = entry.Value;
// Do something here
}

## Method 3
Using foreach non-lazy coding (statically typed) as follows:

C#

foreach (KeyValuePair entry in profilingDictionary)
{
int key = entry.Key;
string value = entry.Value;
// Do something here
}

# Performance Difference Between The Three Methods
After running the performance test several times, no doubt the staticaly typed foreach iteration over a Dictionary variable is by far the best performing out of the three.