-1

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?

markzzz
  • 47,390
  • 120
  • 299
  • 507
  • What would you like to assign to `header_parameters`? A new dictionary? – Adam Houldsworth Oct 09 '13 at 10:24
  • @AdamHouldsworth: I assume he wants to have the `KeyValuePair` of the last inserted item. (_"Why can't I add a new pair..."_) – Tim Schmelter Oct 09 '13 at 10:30
  • @TimSchmelter Others assume he wants a new dictionary with that item added. Hence my question. I don't like assumptions. – Adam Houldsworth Oct 09 '13 at 10:31
  • @AdamHouldsworth: I don't like `var` in this case. But method chaining is a known pattern. http://stackoverflow.com/questions/1119799/method-chaining-in-c-sharp Also, markzzz is a fan of `KeyValuePairs` http://stackoverflow.com/q/19251781/284240 ;) – Tim Schmelter Oct 09 '13 at 10:35
  • @TimSchmelter I know it is, but there are still assumptions about what is being returned to be chained. Now there is an accepted answer I suppose we know what was wanted :-) – Adam Houldsworth Oct 09 '13 at 10:38

3 Answers3

2

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Add method adds item to dictionary, but returns nothing, so you can't assign result of Add method to variable.

signature_parameters.Add("oauth_signature", Uri.EscapeDataString(oauth_signature))

You can assign it later, but because it is a reference type, you'll just copy the reference.

var header_parameters = signature_parameters;
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0

Add is a void method since it adds an item to the dictionary but it does not return something.

So change this

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

to

var header_parameters = new KeyValuePair<string, string>("oauth_signature", Uri.EscapeDataString(oauth_signature));
signature_parameters.Add(header_parameters.Key, header_parameters.Value);

( assuming that you want to have the last inserted KeyValuePair )

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939