Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
好的C#收藏_C#_.net_Arrays_Collections - Fatal编程技术网

好的C#收藏

好的C#收藏,c#,.net,arrays,collections,C#,.net,Arrays,Collections,用C#存储以下数据的好方法是什么: 我有一些复选框,其中包含与每个复选框相关联的subjectId、varnumber、varname和title 我需要一个可以是任何大小的集合,比如ArrayList,可能带有: list[i][subjectid] = x; list[i][varnumber] = x; list[i][varname] = x; list[i][title] = x; 有什么好主意吗?您可能希望为此使用一个,并在数组的第二

用C#存储以下数据的好方法是什么:

我有一些复选框,其中包含与每个复选框相关联的subjectId、varnumber、varname和title

我需要一个可以是任何大小的集合,比如ArrayList,可能带有:

      list[i][subjectid] = x;
      list[i][varnumber] = x;
      list[i][varname] = x;
      list[i][title] = x;

有什么好主意吗?

您可能希望为此使用一个,并在数组的第二维度中为每个值分配位置。例如,
list[i][0]
将是
subject
list[i][1]
将是
varnumber
,等等。

A
list
其中Mumble是一个存储属性的小助手类

List<Mumble> list = new List<Mumble>();
...
var foo = new Mumble(subjectid);
foo.varnumber = bar;
...
list.Add(foo);
,..
list[i].varname = "something else";
List List=新列表();
...
var foo=新的Mumble(主语);
foo.varnumber=bar;
...
添加(foo);
,..
列表[i].varname=“其他内容”;

一个通用列表,
列表
会很好-你的类有subjectid、varnumber等属性。

确定什么集合,通常从你想用它做什么开始


如果您的唯一标准是任意大小,那么我会考虑<代码>列表>代码>

,因为这是一个键,值对,我建议您使用基于泛型的集合。
// Create a new dictionary of strings, with string keys, 
// and access it through the IDictionary generic interface.
IDictionary<string, string> openWith = 
    new Dictionary<string, string>();

// Add some elements to the dictionary. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
//使用字符串键创建一个新的字符串字典,
//并通过IDictionary通用接口访问它。
IDictionary openWith=
新字典();
//向字典中添加一些元素。没有
//重复的键,但某些值是重复的。
openWith.Add(“txt”、“notepad.exe”);
openWith.Add(“bmp”、“paint.exe”);
openWith.Add(“dib”、“paint.exe”);
openWith.Add(“rtf”、“wordpad.exe”);

正如其他人所说,您最好创建一个类来保存这些值,以便列表返回一个包含所需所有数据的对象。虽然二维数组可能很有用,但这看起来不像是那种情况


有关更好的解决方案的更多信息,以及为什么在本例中二维数组/列表不是一个好主意,您可能需要阅读:

如果外部有可能
[i]
的顺序不在可预测的顺序中,或者可能存在间隙,但您需要将其用作键:

public class Thing
{
    int SubjectID { get; set; }        
    int VarNumber { get; set; }
    string VarName { get; set; }
    string Title { get; set; }
}

Dictionary<int, Thing> things = new Dictionary<int, Thing>();
dict.Add(i, thing);

第一维度会变大变小吗?还是我必须提前分配一个特定的尺寸。不这不是一个很好的解决方案,因为索引是弱类型的。(也就是说,你必须知道索引0是主观性的)。我目前还没有掌握C#的最新进展,但我相信你必须提前分配一个特定的大小。但是,如果需要,可以通过多种方法更改数组的长度。方法是,实际上不需要二维数组,因为第二个维度看起来像类上的属性,因此所有属性在第一个维度上都是按组关联的,而不是按位置关联的。这也可能有帮助:dictionary不会很好地工作,因为subjectid、varnumbers都作为唯一主键绑定在一起,所以不能让dictionary dictSubject使用相同的主键(这就是我遇到的问题)。我通常使用Hans方法,因为它允许您强式键入整个集合,因此这可能是最佳选择。然后可以使用枚举来表示键。有关mumble类的帮助,请参阅下面的
// Create a new dictionary of strings, with string keys, 
// and access it through the IDictionary generic interface.
IDictionary<string, string> openWith = 
    new Dictionary<string, string>();

// Add some elements to the dictionary. There are no 
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
public class Thing
{
    int SubjectID { get; set; }        
    int VarNumber { get; set; }
    string VarName { get; set; }
    string Title { get; set; }
}

Dictionary<int, Thing> things = new Dictionary<int, Thing>();
dict.Add(i, thing);
var myThing = things[i];