Does the queue have a link or object value?

In C #, do the following “containers” contain a link or object for reference types?

Queue
Stack
Array
List
Vector

For example, if I do the following:

Queue<MyItem> item = new Queue<MyItem>(100);
MyItem mit = new MyItem();
item.Enqueue(mit);

Is the link mitcopied to itemor the object itself mittransferred to the memory cell item?

if i say

item = null;

it will not set all objects inside itemequal to zero. I'm right?

+3
source share
4 answers

The queue contains a link to the items in the list. Setting the queue to zero does not affect the elements themselves.

+5
source

, MyItem ( struct ) refernce type (class). (.. ), .

+2

mit ?

  • , , .

  • " " . "mit" - . mit2 = mit;, mit, mit2 .

  • , " mit... " . item.Enqueue(mit2);, , .

0

mit mit ?

Not really. If the object in question is a reference type, it does not move or copy. More precisely, a new link to the same object has been added to the collection. If the object is a value type, it is actually copied.

if I say item = null;, this will not set all objects inside the element to null. I'm right?

You're right. It will do nothing with the elements contained in the collection, regardless of whether they are reference or value.

0
source

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


All Articles