This seems like a straightforward thing to do, but I don’t seem to be able to work out the correct syntax. I currently have this:
KeyValuePair<string, string>[] kvpArr = new KeyValuePair<string,string>[];
However, this seems to work:
KeyValuePair<string, string>[] kvpArr = new KeyValuePair<string,string>[10];
But I don’t know the size of the array initially. I know I can use a list of KVPs and I probably will, but I just wanted to know how / if this could actually be done.
No, you can’t do this – because an array always has a fixed size. If you don’t specify that size to start with, what size would you expect to be used? You either have to specify the size itself or the contents (which allows the size to be inferred). For example:
List<T>is definitely your friend for collections where you don’t know the size to start with.