【发布时间】:2020-05-31 05:26:09
【问题描述】:
我初始化这样的东西
public Dictionary<int, HashSet<string>[]> historgram = new Dictionary<int, HashSet<string>[]>()
{
{0, new HashSet<string>[3]},
{1, new HashSet<string>[3]},
{2, new HashSet<string>[3]},
};
然后
if (historgram.TryGetValue(daysAgo, out data))
{
if (t.isTestAutomated)
{
if (data[0] == null)
data[0] = new HashSet<string>();
data[0].Add(t.id);
}
以上工作。如果我有数据要放,我会在运行时初始化
一切都完成后,我编写了一个 Json,并在那些我没有任何东西可放入的实例中以空值结束。它看起来像这样
"historgram": {
"0": [ null, null, null ],
"1": [ null, null, null ],
"2": [ null, null, null ],
"3": [ null, null, null ],
"4": [
[ "XXXX-2244" ],
null,
null
],
我想以空 [] 结尾。如何立即将 HashSet 预初始化为空?
【问题讨论】:
-
这似乎是正确的。 数组长度为3..且3个值全部为null。数组的容量和长度是一样的。例如,如果使用 List<..>,我希望序列化能够尊重集合的(动态)长度。否则,也许是自定义转换器?这将取决于 JSON 序列化..
-
@user2864740,确实如此。我想知道如果可能的话如何显示 [](初始化为空)而不是 null。
-
初始化数组长度为0?
-
@LasseV.Karlsen 怎么样?
标签: c# arrays dictionary hashset