I want to implement a graph with an array of List<int>, but when I try to initialize the entries with Array.Initialize() with the default values (by calling a parameterless constructor of List<int>), it's like the method was never called and after that all entries are still null!
int n = int.Parse(Console.ReadLine()); // number of nodes
List<int>[] g = new List<int>[n]; //graph
g.Initialize();
It's fine when I iterate over the collection and intitialize, but why is there a problem with the method Initialize()?
Don't use
Array.Initialize, ever. Read the documentation on the function, it has an entire page of warnings, including a huge red caution box.Instead initialize your array as you would any other: