将项添加到c#中的字符串数组列表

时间:2015-02-24 04:40:57

标签: c# asp.net

我尝试将字符串数组添加到字符串数组列表

我尝试list.add但没有工作

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

3 个答案:

答案 0 :(得分:7)

您确定内部数组中需要多于1个维度吗?

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

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

如果是,那么:

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

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

但我宁愿有自定义类型:

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 }
};

答案 1 :(得分:2)

您需要创建rectangular array,但是您尝试传递single dimensional array个字符串而不是矩形数组。

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

答案 2 :(得分:2)

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

您错过了项目周围的括号

相关问题