As people already said, an array (as in most languages) are not resizeables.
I think it's interesting to see how you instanciate an array in C to understand that.
In C either you instanciate like that
int[5] myArray
or you choose the dynamic way with malloc pretty much like so
int *myArray = malloc(5 *sizeof(int));
What you can see is that when you instanciate them you have to give it a size and it will "lock" a chunck of memory for it.
Now if you want to add more things to it you have to recreate a new one bigger and copy the content from one to another.
Lists work in a different way. each element of a list are alocated separatly and each of them contain a pointer to the next element.
That's why it's way easier to add an element to a list. What happens under the hood is that it creates that element and alocate memory for it and make the last (or first it depends) element of the list to it (you can see it as a chain)
It should remind you of what you're trying to achieve with your tree. Graphs and lists aren't much different but graph doesn't already exist int c# so you have to recreate it.
Simple answer
So the easy solution for you case is to swap the array by a list like so:
Dictionary<string, List<string>> graph
and then you use it like so
public void AddEdge(string NodeName, string EdgeName)
{
graph[NodeName].Add(EdgeName);
}
More complicated answer
So following what we said about list, we should assume that each element of your graph should reference other elements.
That will look like so:
using System.Text.Json;
var edge0 = new Edge() { WhatEverData = "level0 : edge0" }; //Your base node/edge
var edge1 = new Edge() { WhatEverData = "level1 : edge1" };
var edge2 = new Edge() { WhatEverData = "level1 : edge2" };
var edge3 = new Edge() { WhatEverData = "level1 : edge3", Edges = new List<Edge> { new Edge() { WhatEverData = "level2" } } };
edge0.AddEdge(edge1);
edge0.AddEdge(edge2);
edge0.AddEdge(edge3);
Console.WriteLine(JsonSerializer.Serialize(edge0));
public sealed class Edge
{
public string? WhatEverData { get; set; }
public List<Edge>? Edges { get; set; }
public Edge AddEdge(Edge toAdd)
{
Edges ??= new List<Edge>();
Edges.Add(toAdd);
return this;
}
}
In this example everything is an edge, Edge0 being the root of your graph but your root could also be a list of edge if you really want to.
The point is that now that it's an object you can put pretty much everything you want in there.
You can also easily serialize it! Here it's what the output of this tiny code looks like:
{
"WhatEverData": "level0 : edge0",
"Edges": [
{ "WhatEverData": "level1 : edge1", "Edges": null },
{ "WhatEverData": "level1 : edge2", "Edges": null },
{
"WhatEverData": "level1 : edge3",
"Edges": [{ "WhatEverData": "level2", "Edges": null }]
}
]
}
Hope it helped