lightest collection possible

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
 
S

Stephany Young

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);
 
C

Cor Ligthert[MVP]

Rodchar,

The lightest collection there is, is probably

bool() A = {};

Cor
 
R

rodchar

How is the dictionary/hashtable different from list<kvp<>> since all are key
value pair anyway right?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top