I'm trying to complete the challenge of creating an XOR linked list. However, I can't finish it because every time I allocate a node object, it uses the same block of memory as the previous one.
var list = new ListXOR();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
Console.WriteLine("Done.");
class ListXOR
{
    private readonly List<Node> _nodes = new();
    public void Add(int value)
    {
        Node node = new(value);
        _nodes.Add(node);
        unsafe
        {
            Console.WriteLine("Address of new node {0}", (ulong)&node);
        }
    }
    private class Node
    {
        public int value;
        public Node(int newValue) => value = newValue;
    }
}
This code displays in the console something like the following:
Address of new node 849654375800
Address of new node 849654375800
Address of new node 849654375800
Address of new node 849654375800
Done.
You also need to add this to your *.csproj file.
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Why does this happen? Is there a way to prevent it?
 
     
     
    