简体   繁体   English

什么是 Javascript 键/值对数组的 C# 等价物

[英]What is the C# equivalent of a Javascript array of key/value pairs

What does this represent in javascript:这在javascript中代表什么:

[["January", 10], ["February", 8], ["March", 4], ["April", 13], ["May", 17], ["June", 9]]

And secondly, what is the C# equivalent?其次,什么是 C# 等价物?

Array of arrays??数组数组?? But each inner array is essentially a key/value pair, so that's not right.但是每个内部数组本质上都是一个键/值对,所以这是不对的。

How would one generate this JS object in C#?如何在 C# 中生成这个 JS 对象?

If it's a map, then the equivalent for efficient lookups in C# would be a Dictionary<TKey, TValue> :如果它是地图,那么 C# 中高效查找的等效项将是Dictionary<TKey, TValue>

var months = new Dictionary<string, int>()
{
    {"January", 1}, {"February", 2}, {"March", 3},
    {"April", 4}, {"May", 5}, {"June", 6}
};

Now, if it's an array of tuples, C# 7 has native tuple support:现在,如果它是一个元组数组,C# 7 有原生元组支持:

var months = new[] 
{
    ("January", 1), ("February", 2), ("March", 3),
    ("April", 4), ("May", 5), ("June", 6)
};

And you can even name the members of the tuple like so:你甚至可以像这样命名元组的成员:

var months = new (string Name, int Number)[]
{
    ("January", 1), ("February", 2), ("March", 3),
    ("April", 4), ("May", 5), ("June", 6)
};

Console.WriteLine(months[0].Name);

Its Dictionary它的Dictionary

Syntax: Dictionary<TKey, TValue>

var myDictionary = new Dictionary<int, string>();
myDictionary.Add(10, "January");
myDictionary.Add(8, "February");
myDictionary.Add(4, "March");
myDictionary.Add(13, "April");
myDictionary.Add(17, "May");

You can also have a List of KeyValuePairs你也可以有一个KeyValuePairs List

var list = new List<KeyValuePair<string, int>>()
{
    new KeyValuePair<string, int>("January", 10),
    new KeyValuePair<string, int>("February", 8),
    new KeyValuePair<string, int>("March", 4),
    new KeyValuePair<string, int>("April", 13),
    new KeyValuePair<string, int>("May", 17),
};

So what is the difference between List<KeyValuePair<T1, T2>> and Dictionary<T1, T2> ?那么List<KeyValuePair<T1, T2>>Dictionary<T1, T2>什么区别?

The answer is the List does not enforce uniqueness of the Key.答案是 List 不强制 Key 的唯一性。

Meaning you can do this with a List and KeyValuePair .这意味着您可以使用ListKeyValuePair执行此操作。

//Record with same key
new KeyValuePair<string, int>("May", 17),
new KeyValuePair<string, int>("May", 17),

But you cannot do it with a Dictionary但是你不能用Dictionary来做

    myDictionary.Add(17, "May");
    myDictionary.Add(17, "May"); //ERROR
[["January", 10], ["February", 8], ["March", 4], ["April", 13], ["May", 17], ["June", 9]]

What does this represent in javascript:这在javascript中代表什么:

Well [] denotes an array.那么[]表示一个数组。 So the outer portion is an array.所以外部是一个数组。 Since each array element is also delimited by [] each element is also an array.由于每个数组元素也由[]分隔,因此每个元素也是一个数组。

And secondly, what is the C# equivalent?其次,什么是 C# 等价物?

Well pragmatically you can't assign that value to anything, it would cause a compile time error.那么务实,你不能分配值的话,这会导致编译时错误。 If you used something like Json.Net to convert it to ac# object, the absolute closest thing (outside -> in) would be a jagged array .如果您使用 Json.Net 之类的东西将其转换为 ac# 对象,那么绝对最接近的事物(外部 -> 内部)将是锯齿状数组

object[][]

Normally jagged arrays are not really that useful.通常锯齿状数组并不是那么有用。 In this case, my opinion would be to covert the array into a List<DateTime> because Lists's are much easier to work with, and it appears that each array element contains a month and a day, which is easier to work with as DateTime object.在这种情况下,我的意见是将数组转换为List<DateTime>因为列表更容易使用,而且每个数组元素似乎都包含一个月和一天,作为 DateTime 对象更容易使用.

What does this represent in javascript:这在javascript中代表什么:

It can be map:它可以是地图:

let m = new Map([["February", 8], ["March", 4], ["April", 13]])

Or just array of arrays:或者只是数组数组:

let a = [["February", 8], ["March", 4], ["April", 13]];

And secondly, what is the C# equivalent?其次,什么是 C# 等价物?

It can be Dictionary<string, int> or array of arrays它可以是Dictionary<string, int>或数组数组

In Javascript it is an Array of Arrays:在 Javascript 中,它是一个数组数组: 在此处输入图片说明

Since C# is a strongly typed language it is unconventional, and not recommended, to represent this data in its current form but you can, like ErikPhillips says in the comments below, like so:由于 C# 是一种强类型语言,因此不推荐使用当前形式表示这些数据,但您可以,就像 ErikPhillips 在下面的评论中所说的那样,如下所示:

object[] array1 = { "February", 8 };

Since the "outer" Array is an Array of two types, string and int, it would be more conventional strongly typed C# best practices to create a "model" of each month day array element in something like the C# code below, since C# arrays are more frequently an array of one type, for example an array of string or an array of int or in the code below, conventional strongly typed C# an array of MonthDayModel:由于“外部”数组是字符串和整数两种类型的数组,因此在类似于下面的 C# 代码中创建每个月日数组元素的“模型”将是更传统的强类型 C# 最佳实践,因为 C# 数组更常见的是一种类型的数组,例如一个字符串数组或一个 int 数组,或者在下面的代码中,传统的强类型 C# 一个 MonthDayModel 数组:

public class TestClass {

    public TestClass(){
        MonthDayModel[] arr = new MonthDayModel[10];
        arr[0] = new MonthDayModel() {
            Month = "February",
            Day = 8
        };
        arr[1] = new MonthDayModel() {
            Month = "March",
            Day = 4
        };
        //...
    }
}

public class MonthDayModel {
    public string Month { get; set; }
    public int Day { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM