C# C“如何”;数组。添加";到字典的数组?

C# C“如何”;数组。添加";到字典的数组?,c#,arrays,dictionary,C#,Arrays,Dictionary,假设你有一本这样的字典: Dictionary<int, int[]> dict = new Dictionary<int, int[]>(); dict.Add(0, new int[]{1, 2, 3, 4}); 所以我有这样一个:Key=0,Value=[1,2,3,4] 但是,如果我想在“Key=0”的值处添加一个“5”,使其看起来像Key=0,value=[1,2,3,4,*5*],会发生什么呢?要修改字典中的数组,可以使用LINQ的Append(): 但如

假设你有一本这样的字典:

Dictionary<int, int[]> dict = new Dictionary<int, int[]>();
dict.Add(0, new int[]{1, 2, 3, 4});
所以我有这样一个:
Key=0,Value=[1,2,3,4]


但是,如果我想在“Key=0”的值处添加一个“5”,使其看起来像
Key=0,value=[1,2,3,4,*5*]
,会发生什么呢?

要修改字典中的数组,可以使用LINQ的
Append()

但如果要修改数组,则不应使用数组。使用
列表

var dict = new Dictionary<int, List<int>>();

dict.Add(0, new List<int> {1,2,3,4});

您需要做的是提取数组,并创建一个新的数组,该数组的末尾带有
5
,因为数组的大小是固定的
var dict = new Dictionary<int, List<int>>();

dict.Add(0, new List<int> {1,2,3,4});
dict[0].Append(5);