如何用c#制作收藏?
我在PHP中有一个关联数组如何用c#制作收藏?,c#,php,C#,Php,我在PHP中有一个关联数组 我想把代码转移到c语言编程中,但我不知道如何转移它。我尝试使用列表,但我只能接受100和500。 如何将其转换为c#编程?使用该类 所以你可以这样做: var dict = new Dictionary<int, Dictionary<string, int[]>> { { 100, new Dictionary<string, int[]>
我想把代码转移到c语言编程中,但我不知道如何转移它。我尝试使用列表,但我只能接受100和500。
如何将其转换为c#编程?使用该类 所以你可以这样做:
var dict = new Dictionary<int, Dictionary<string, int[]>>
{
{
100, new Dictionary<string, int[]>
{
{"first", new[] {4, 24, 5, 0}},
{"second", new[] {42, 58, 23, 8}}
}
},
{
200, new Dictionary<string, int[]>
{
{"first", new[] {4, 24, 5, 0}},
{"second", new[] {42, 58, 23, 8}}
}
}
};
public class DataContainer {
public int Index { get; set; }
public DataValue MyValue { get; set; }
}
public class DataValue {
public string Name { get; set; }
public List<int> IntegerValues { get; set; }
}
var dict=新字典
{
{
100,新字典
{
{“第一”,新[{4,24,5,0},
{“第二”,新[{42,58,23,8}
}
},
{
200,新字典
{
{“第一”,新[{4,24,5,0},
{“第二”,新[{42,58,23,8}
}
}
};
然后您可以简单地拥有一个数据容器列表:
var data=newlist()代码>我想你在找字典:
这是我认为您发布的内容的1对1映射(您也可以用int[]替换列表):
注意:我确实看到你的内部字典总是相同的,有两个内部字典是没有意义的,但是就像我说的,它是我看到的东西的1对1映射
var maindirectionary=newdictionary();
var firstInnerDictionary=新字典();
var secondInnerDictionary=新字典();
添加(“first”,新列表{4,24,5,0});
mainDictionary.Add(100,firstInnerDictionary);
添加(“第一”,新列表{4,24,5,0});
maindirectionary.Add(500,firstInnerDictionary);
PHP的关联数组
映射到字典
类的C
。数值数组映射到列表
类,或者映射到更直接的数组
(例如int[],string[])
C#
和PHP
之间的一个主要区别是C#
是一种强类型的编程语言,这意味着您需要声明变量的类型。然后可以使用文本语法来构建字典
var data = new Dictionary<int, Dictionary<string, int[] > >()
{
[100] = new Dictionary<string, int[] >()
{
["first"] = new int[] {4, 24, 5, 0},
["second"] = new int[] {42, 58, 23, 8}
// the rest of the items
}
// the rest of the items
};
我的建议是使用字典
,如下所示:
var data = new Dictionary<int, Dictionary<string, List<int>>>;
data[100] =new Dictionary<string, List<int>>();
data[100]["first"] = new List<int>();
data[100]["first"].AddRange(new [] {4, 24, 5, 0});
// etc...
var数据=新字典;
数据[100]=新字典();
数据[100][“第一”]=新列表();
数据[100][“first”].AddRange(新[]{4,24,5,0});
//等等。。。
不过,还有字典和列表初始值设定项使代码更加紧凑 列表和字典是最好的选择
首先,您应该确保使用的是泛型集合命名空间:
using System.Collections.Generic;
在文件的顶部
然后,您可以创建整数列表字典,如下所示:
var data = new Dictionary<int,Dictionary<string,List<int>>>() {
{100, new Dictionary<string, List<int>>() {
{"first", new List<int>() {4, 24, 5, 0}},
{"second", new List<int>() {42, 58, 23, 8}} //TODO - add third, fourth etc.
}},
{500, new Dictionary<string, List<int>>() {
{"first", new List<int>() {4, 24, 5, 0}},
{"second", new List<int>() {42, 58, 23, 8}} //TODO - add third, fourth etc.
}}
}
var data=newdictionary(){
{100,新词典(){
{“第一”,新列表(){4,24,5,0},
{“第二”,新列表(){42,58,23,8}//TODO-添加第三、第四等。
}},
{500,新词典(){
{“第一”,新列表(){4,24,5,0},
{“第二”,新列表(){42,58,23,8}//TODO-添加第三、第四等。
}}
}
您使用List
做出了正确的选择,您在实现中遇到了什么问题?请说明您在c#
@Ian AList
中实际尝试了什么,它不是一个关联数组,那么是什么让您认为它是合适的呢?@Servy我应该说,List
是可以的,我想。。。也许right
是一个很强的词(可能是因为缺乏更好的理解)。但我的理由是,数据集很小,List
很容易处理,如果需要,您也可以将KVP放入List
中。也许有更好/更有效的选择超出了我的知识范围。但是对于给定的情况,我会说列表 是可接受的。考虑创建用数组来表示您试图建模的概念的类。看起来,外部字典的键是整数,而不是字符串。内部字典的值是一个int数组,而不仅仅是一个int。这只是一个示例。但我刚刚用正确的实现进行了更新。在C#6中使用新的字典初始值设定项更好。应该注意的是,对于这种类型的字典初始化,您需要C#6。@juharr感谢您的通知,将更新答案
var data = new Dictionary<int, Dictionary<string, int[] > >()
{
{ 100, new Dictionary<string, int[] >()
{
{"first", new int[] {4, 24, 5, 0} },
{"second", new int[] {42, 58, 23, 8} }
// the rest of the items
}
}
// the rest of the items
};
var data = new Dictionary<int, Dictionary<string, List<int>>>;
data[100] =new Dictionary<string, List<int>>();
data[100]["first"] = new List<int>();
data[100]["first"].AddRange(new [] {4, 24, 5, 0});
// etc...
using System.Collections.Generic;
var data = new Dictionary<int,Dictionary<string,List<int>>>() {
{100, new Dictionary<string, List<int>>() {
{"first", new List<int>() {4, 24, 5, 0}},
{"second", new List<int>() {42, 58, 23, 8}} //TODO - add third, fourth etc.
}},
{500, new Dictionary<string, List<int>>() {
{"first", new List<int>() {4, 24, 5, 0}},
{"second", new List<int>() {42, 58, 23, 8}} //TODO - add third, fourth etc.
}}
}