0

I'd like to define a list whose element is a 3-elements array. Below codes seems ok: List<dynamic[]> bb = null; but when I try: List<dynamic[3]> bb = null;

It throws error. Sure I can create a class/struct for that. But is there a way to define it directly?

3
  • Error CS0305 Using the generic type 'List<T>' requires 1 type arguments Commented Jul 19, 2018 at 4:00
  • 2
    Every time i see someone use dynamic i cringe Commented Jul 19, 2018 at 4:16
  • Do either of these work List<dynamic[]> bb1 = new List<dynamic[]> { new dynamic[] { 1, "a", "h" } }; List<dynamic[]> bb2 = new List<dynamic[]> { new dynamic[3] };? Commented Jul 19, 2018 at 4:16

4 Answers 4

5

Here is one way:

var list = new List<string[]>();

list.Add(new string[3] { "1", "2", "3" });
list.Add(new string[2] { "1", "2" });

Update:

@Ilya's answer shows a solution with dynamic, but I advise you against using dynamic. If you know the structure of the objects create a class or use a tuple, e.g.

var list = new List<(int id, string name, uint reputation)>();

list.Add((298540, "xiaoyafeng", 11));
list.Add((2707359, "Ilya", 3576));
list.Add((581076, "tymtam", 4421));
list.Add((3043, "Joel Coehoorn", 294378));
Sign up to request clarification or add additional context in comments.

5 Comments

I absolutely agree with you! It is better to avoid dynamic when it is possible (at least when it is not obvious how to create a list of arrays). The only reason I put dynamic in my answer is in using of this in the question.
It's very helpful! Thanks!
Tuple is the way to go here, I think.
btw, for the best practice, which one is better? List<(int id, string name, uint reputation)> or List<Tuple<int, string, uint>> ?
Best practice would probably be a class. Cause eventually you'll use it. But it's hard to know without a lot more context.
1

I'm not sure what your issue was; your title talks about a "string array list", but your posted code describes a list of arrays of dynamic.

I'm not sure I'll ever have reason to create a list of arrays of dynamic, but to show that the code that you discussed in your post can work, this works for me:

var stuff = new List<dynamic[]>
{
    new dynamic[] {1, "string"},
    new dynamic[] {DateTime.Now, 45.0}
};

But, as noted in other answers, dynamic is a great answer to several classes of questions. But, it's not the right answer here. The right answer to the title of your question (not the description) will use a list of string arrays (as has been pointed in the other answers:

var otherStuff = new List<string[]>
{
    new string[] {"Now", "Is", "the", "time"},
    new string[] {"for all", "good men, etc."}
};

2 Comments

Why the down vote? I was simply pointing out that the OP's original question (which was muddled) had sample code, and that there is a way to get that sample code to compile. His question title and his description don't match (the title was about a "string array list", while the code he posted in his was about a list of arrays of dynamic) If someone searches for a list of array of dynamic, my answer at least shows it's possible. I'll edit the answer to make it clear what I'm doing. But, if you down-vote me, at least have the courtesy to tell my I'm crazy - thanks!
I didn't down vote, but yeah i think it was a little harsh as well, upvote for getting it to work
0

It seems that you need something like this:

List<dynamic[]> bb = new List<dynamic[]>();
bb.Add(new dynamic[3]); // here you add a 3-element array
bb.Add(new dynamic[3] { "a", "b", "c" }); // here you add another one 3-element array

Comments

0
// List containing a string array with 3 nulls in it
var aa = new List<string[]> {new string[3]};

// List containing a string array with 3 strings in it
var bb = new List<string[]> {new[] {"a", "b", "c"}};

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.