Good day! It is required to create a linked list of structure objects in which the current element should point to the next one, the last one to null. In C ++, when I did this, there were no problems. In C #, the first element is created without problems. But the second is created with the same address! How to fix it?
// Структура объектов очереди struct QueueItem { public int Value; public unsafe QueueItem* Next; public unsafe QueueItem(int value) : this() { Value = value; Next = null; } } class QueueWithMinStats { private unsafe QueueItem* _first; private unsafe QueueItem* _last; public unsafe QueueWithMinStats() { _first = null; _last = null; } // Добавить в очередь значение public unsafe void Enqueue(int value) { // Проблема тут!!! var newItem = new QueueItem(value); var newItemLink = &newItem; if (_first == null) // Если вводимый элемент - первый _first = newItemLink; else _last->Next = newItemLink; _last = newItemLink; } }
unsafe
if you do not understand what this keyword means. And native pointers. Otherwise, you yourself create a problem. - VladD 5:09