lightest collection possible

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,
i have a list of keyvalue pairs i'd like to collect in a generic collection

currently i'm using:

private List<KeyValuePair<string,string>> myCol ... ;

and then to add:
myCol.Add(new KeyValuePair<string,string>(val1,val2));

is this the lightest collection there is? i'm kinda wondering about having
to instance a new kvp on each add.

thanks,
rodchar
 
What is it that you are concerned about?

Are you perhaps concerned about havaing to type 'new
KeyValuePair<string,string>(val1,val2)' repeatedly?

Or are you concerned about performance?

If it is the latter, then the the simple answer is that the only way you can
add an 'item' to a List<KeyValuePair<string,string>> collection is to
instantiate an instance of KeyValuePair<string,string>. This is the whole
idea of strongly typed collections.

If it is the former, then you could roll your own derivation of
List<KeyValuePair<string,string>> to ease the amount of typing you have to
do:

public class MyCollection : List<KeyValuePair<string,string>>
{
public void Add(string val1, string val2)
{
base.Add(new KeyValuePair<string,string>(val1,val2));
}
}

private MyCollection myCol = new MyCollection();

myCol.Add(val1,val2);
 
How is the dictionary/hashtable different from list<kvp<>> since all are key
value pair anyway right?
 
Back
Top