1

I try to add a string array to a list of string arrays

I tried list.add but didnt work

List<string[,]> stringList=new List<string[,]>();
stringList.Add({"Vignesh","26"},{"Arul","27"});

3 Answers 3

7

Are you sure you need more than 1 dimension in the inner arrays?

List<string[]> stringList = new List<string[]>(); // note just [] instead of [,]
stringList.Add(new string[] { "Vignesh", "26" } );
stringList.Add(new string[] { "Arul", "27" } );

or

List<string[]> stringList = new List<string[]>
{
    new string[] { "Vignesh", "26" }
    new string[] { "Arul", "27" } 
};

If yes then:

List<string[,]> stringList = new List<string[,]>();
stringList.Add(new string[,] { { "Vignesh" }, { "26" } } );
stringList.Add(new string[,] { { "Arul" }, { "27" } } );

or

List<string[,]> stringList = new List<string[,]>
{
    new string[,] { { "Vignesh" }, { "26" } },
    new string[,] { { "Arul" }, { "27" } }
};

But I'd rather have a custom type:

class Person
{
    public string Name { get; set; }

    public int Age { get; set; } // or of type string if you will
}

List<Person> personList = new List<Person>
{
    new Person { Name = "Vignesh", Age = 26 }
};
Sign up to request clarification or add additional context in comments.

Comments

2

You need to create rectangular array but you are trying to pass single dimensional array of strings instead of rectangular array.

List<string[,]> stringList=new List<string[,]>();
stringList.Add(new string[,] {{"Vignesh","26"},{"Arul","27"}});

Comments

2
List<string[,]> list = new List<string[,]> ();
list.Add(new string[,] { {"Vignesh","26"},{"Arul","27"} });

You're missing a bracket around your items

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.