Triynko
Triynko

Reputation: 19204

Is the RouteValueDictionary class's keys case-insensitive?

The documentation states that it "Represents a case-insensitive collection of key/value pairs that you use in various places in the routing framework, such as when you define the default values for a route or when you generate a URL that is based on a route."

From what I can see, it uses a normal Dictionary<string,object> to store it's keys internally, so it's actually case-sensitive. I cannot find any aspect of this class that's case-insensitive, so is the documentation just wrong?

Upvotes: 0

Views: 796

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

Your statement that using Dictionary<string, object> makes the comparison case-sensitive is incorrect. Dictionary<TKey, TValue> allows for a custom IEqualityComparer<TKey> implementation to be provided into the constructor. RouteValueDictionary does exactly that and passes StringComparer.OrdinalIgnoreCase to Dictionary<string, object> constructor:

public class RouteValueDictionary : IDictionary<string, object> {
    private Dictionary<string, object> _dictionary;

    public RouteValueDictionary() {
        _dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
    }

    public RouteValueDictionary(object values) {
        _dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

        AddValues(values);
    }

    public RouteValueDictionary(IDictionary<string, object> dictionary) {
        _dictionary = new Dictionary<string, object>(dictionary, StringComparer.OrdinalIgnoreCase);
    }

So to answer the question: RouteValueDictionary is case-insensitive.

See the code on sourceof.net: https://referencesource.microsoft.com/#System.Web/Routing/RouteValueDictionary.cs,551a423c96bb6648

Upvotes: 3

Related Questions