0

I have a list and I want to copy three other lists into it.

// The main list
List<List<string>> list= new List<List<string>>();

// The lists which I want to combine
ArrayList sublist1= new ArrayList();;
ArrayList sublist2= new ArrayList();;
ArrayList sublist3= new ArrayList();;

What I tried is:

list[0].AddRange(sublist1);
list[0].AddRange(sublist2);
list[0].AddRange(sublist3);

It doesn't work because It is multidimensional list. I need this type of list for the future plans.

How can I accomplist it?

4
  • 1
    I suspect it doesn't work for other reasons (not creating instances, missing type-specificity, adding sublist2 twice, etc).. what is the real code and what is the error message / exception / incorrect result / unexplained observed behavior? (And what is the desired result?) Commented Aug 7, 2012 at 1:37
  • It says The best overloaded method match for 'System.Collections.Generic.List<string>.AddRange(System.Collections.Generic.IEnumerable<string>)' has some invalid arguments Commented Aug 7, 2012 at 1:41
  • So there you have it. You need to pass in something conforming to IEnumerable<string>. ArrayList does not. Nothing to do with multidimensionalisms. Perhaps: list[0].AddRange(sublist1.Cast<string>()) (make sure to "using" Linq) .. or better, well-type the "sublists" (e.g. List<string> sublist1 = ..). Commented Aug 7, 2012 at 1:41
  • Yes it worked! thanks @pst but I can't vote your comment for some reason Commented Aug 7, 2012 at 3:00

3 Answers 3

1

As already mentioned in comments just use List<string> instead of ArrayList. It has nothing to do about multidimensional arrays, just types mismatch.

Then you say List<List<string>> it basically means create list type, which will contain List<string> as items (the part in angle brackets), so you need to add them, not ArrayLists. Similarly List<string> means type of list which will contain string as items.

Sign up to request clarification or add additional context in comments.

Comments

1

As in the comments, you need to pass a type that follows IEnumerable. For example, you can change your ArrayLists to List

// The main list
List<List<string>> list = new List<List<string>>();

// The lists which I want to combine
var sublist1 = new List<string>();
var sublist2 = new List<string>();
var sublist3 = new List<string>();

list[0].AddRange(sublist1);
list[0].AddRange(sublist2);
list[0].AddRange(sublist3);

Comments

1

change type of sublists to some IEnumerable<string> (string[] or List<string> or something else)

var sublist1 = new string[] {};
var sublist2 = new string[] {};
var sublist3 = new string[] {};

OR do cast

list[0].AddRange((IEnumerable<string>) sublist1);
list[0].AddRange((IEnumerable<string>) sublist2);
list[0].AddRange((IEnumerable<string>) sublist3);

Because you are trying to use AddRange method of System.Collections.Generic.List<T> and the signature of this method is

public void AddRange(System.Collections.Generic.IEnumerable<T> collection)

so it requires IEnumerable as a parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.