Same address returned in C # struct pointer

I created a structure pointer, but each call to a new node returns the same address. But I was expecting a different address to be returned for each call to the new node. Can anybody help?

public unsafe struct Node
{
    public int Data;
}
class TestPointer
{
    public unsafe Node* getNode(int i)
    {
        Node n = new Node();
        n.Data = i;
        Node* ptr = &n;
        return ptr;
    }
    public unsafe static void Main(String[] args)
    {
        TestPointer test = new TestPointer();
        Node* ptr1 = test.getNode(1);
        Node* ptr2 = test.getNode(2);
        if (ptr1->Data == ptr2->Data)
        {
            throw new Exception("Why?");
        }
    }
}
+3
source share
2 answers

Node n = new Node();! Node , n . getNode , , , . ( "" ) getNode, , n, . : . , CLR , Node .

+4

n ?

0

Source: https://habr.com/ru/post/1776839/


All Articles