Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IDictionary<string, string> or NameValueCollection

Tags:

c#

I'm currently writing an interface to allow applications to send exception data to a central repository for support purposes. I'm at a quandary about how to pass extra contextual data:

public interface IExceptionNotifier
{
    void Notify(Exception ex, NameValueCollection context); //this      
    void Notify(Exception ex, IDictionary<string, string> context); //or this
}

I've often found myself is a similar position when creating lookups. Ignoring whether or not the exception notifier concept is good, is it be best to use an IDictionary<string, string> or NameValueCollection? Why would you pick one over the other?

like image 677
Ben Shepheard Avatar asked Sep 27 '08 21:09

Ben Shepheard


1 Answers

(Assuming .NET 3.5 here. Pre .NET 3.5, NameValueCollection would have the benefit of being able to tie multiple values to a key, with no direct equivalent in "normal" collections.)

Do you want keys to have potentially multiple values? If so, I'd consider ILookup<TKey, TValue>. Otherwise, I'd go for IDictionary<TKey, TValue>.

Aside from anything else, if the receiver wants to do any processing where LINQ would be of use to them, either of the generic interfaces is going to be nicer than NameValuePair. Likewise, it may be easier for the caller to produce a dictionary/lookup using LINQ if they've already got a generic or dynamic kind of context.

like image 92
Jon Skeet Avatar answered Oct 04 '22 21:10

Jon Skeet