C# 有没有办法定义两个元素字符串数组的 List<> ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/286399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:42:01  来源:igfitidea点击:

Is there a way to define a List<> of two elements string array?

c#arraysgenericsdata-structures

提问by Alexander Prokofyev

I want to build two-dimentional array of strings where length of one dimention is 2. Similar to this

我想构建二维字符串数组,其中一维的长度为 2。与此类似

string[,] array = new string[,]
{
    {"a", "b"},
    {"c", "d"},
    {"e", "f"},
    {"g", "h"}
}

Doing

正在做

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

list.Add(new string[2] {"a", "b"});
list.Add(new string[2] {"c", "d"});
list.Add(new string[2] {"e", "f"});
list.Add(new string[2] {"g", "h"});

list.ToArray();

gives me

给我

string[][]

but not

但不是

string[,] 

array.

大批。

Just curious, is there some trick to build dynamically

只是好奇,是否有一些动态构建的技巧

string[,] 

array somehow?

数组不知何故?

采纳答案by Jon Skeet

Well, you could reasonably easily write an extension method to do it. Something like this (only tested very slightly):

好吧,你可以很容易地编写一个扩展方法来做到这一点。像这样(只测试非常轻微):

public static T[,] ToRectangularArray<T>(this IEnumerable<T[]> source)
{
    if (!source.Any())
    {
        return new T[0,0];
    }

    int width = source.First().Length;
    if (source.Any(array => array.Length != width))
    {
         throw new ArgumentException("All elements must have the same length");
    }

    T[,] ret = new T[source.Count(), width];
    int row = 0;
    foreach (T[] array in source)
    {
       for (int col=0; col < width; col++)
       {
           ret[row, col] = array[col];
       }
       row++;
    }
    return ret;
}

It's a slight shame that the above code uses T[] as the element type. Due to generic invariance I can't currently make source IEnumerable<IEnumerable<T>>which would be nice. An alternative might be to introduce a new type parameter with a constraint:

上面的代码使用 T[] 作为元素类型有点遗憾。由于通用不变性,我目前无法制作IEnumerable<IEnumerable<T>>很好的源代码。另一种方法可能是引入一个带有约束的新类型参数:

public static T[,] ToRectangularArray<T,U>(this IEnumerable<U> source)
    where U : IEnumerable<T>

Somewhat hairy, but it should work. (Obviously the implementation needs some changes too, but the basic principle is the same.)

有点毛茸茸的,但它应该工作。(显然实现上也需要一些改动,但基本原理是一样的。)

回答by Andrew Kennan

This isn't possible with a List<string[]>, as the type string[,]is different from string[].

这对 a 来说是不可能的List<string[]>,因为类型string[,]string[].

回答by Robert Wagner

The only way to do it would be to implement the ToArray()function yourself. You could implement it within your own collection (i.e. StringTupleCollection). This could work the same as ArrayList(i.e. internal array increasing in size as needed).

唯一的方法是自己实现该ToArray()功能。您可以在您自己的集合中实现它(即StringTupleCollection)。这可以与ArrayList(即根据需要增加内部数组的大小)相同。

However I'm not sure the advantage of [x,2]over [x][2](or even List<string[2]>would be significant enough to warrant the effort.

但是,我不确定[x,2]over的优势[x][2](甚至List<string[2]>是否足以保证付出努力。

You could also write a StringTuppleclass as:

您还可以将StringTupple类编写为:

public class StringTupple : KeyValuePair<string, string>
{
}

回答by Terrence

You can do this.

你可以这样做。

List<KeyValuePair<string, string>>

The idea being that the Key Value Pair would mimic the array of strings you replicated.

这个想法是键值对将模仿您复制的字符串数组。

回答by hagensoft

KeyValuePair did not work for me when I had to retrieve the values of the checkboxes on the controller as my model.Roles list was null.

当我必须检索控制器上复选框的值时,KeyValuePair 对我不起作用,因为我的 model.Roles 列表为空。

foreach (KeyValuePair<string, bool> Role in model.Roles){...}

The KeyValuePair structure doesn't have a default parameterless constructor and can't be instantiated by the model binder. I recommend a custom model class for your view that has just those properties. ASP.NET MVC 3 binding user control of type KeyValuePair to ViewModel

KeyValuePair 结构没有默认的无参数构造函数,并且不能被模型绑定器实例化。我为您的视图推荐一个只有这些属性的自定义模型类。ASP.NET MVC 3 将 KeyValuePair 类型的用户控件绑定到 ViewModel

I found an implementation of a checkboxlist without the use of html helper at the following link CheckboxList in MVC3.0

在 MVC3.0 中的以下链接CheckboxList 中找到了不使用 html helper 的 复选框列表的实现