Search code examples
c#.netsorteddictionary

Why can't I add a new pair into a SortedDictionary?


I have this SortedDictionary:

var signature_parameters = new SortedDictionary<string, string>
{
    { "oauth_callback", SocialEngine.twitter_aggrega_redirect_uri },
    { "oauth_consumer_key", SocialEngine.twitter_aggrega_consumer_key },
};

now I tried this:

var header_parameters = signature_parameters.Add("oauth_signature", Uri.EscapeDataString(oauth_signature));

but it says I can't assign a void variable to a implicit method.

Where am I wrong?


Solution

  • Where am I wrong?

    You're calling the Add method, but expecting it to return something. It doesn't.

    If you're trying to create a new dictionary with the same entries and one new one, you probably want:

    var headerParameters = new SortedDictionary<string, string>(signatureParameters)
    {
        "oauth_signature", Uri.EscapeDataString(oauth_signature)
    };
    

    The constructor will create a copy of the existing dictionary, then the collection initializer will add the key/value pair just to the new one.