samuelneff/MimeTypeMap

Redundent code

Milade opened this issue · 1 comments

I don't understand why you didn't simply return mappings and have written this code?

`var cache = mappings.ToList(); // need ToList() to avoid modifying while still enumerating

        foreach (var mapping in cache)
        {
            if (!mappings.ContainsKey(mapping.Value))
            {
                mappings.Add(mapping.Value, mapping.Key);
            }
        }
        return mappings;`

When this is useful?

Look at the loop more carefully.

                mappings.Add(mapping.Value, mapping.Key);

It's adding reverse mappings.. so the dictionary contains both mappings of extensions to mime types and mime types to extensions. Since a single mime type can be used for multiple extensions, we make sure we store the first encountered mime type only. For most of these situations, we've pre-defined the reverse mapping to make sure it's the preferred one.

Thanks for asking, that code could use a comment.

Sam