[Solved] Why there are “null” in c#? [closed]


The problem why you sometimes get null is the concurrent access to your list with the operations Add and ElementAt. Since in your case ElementAt won’t have to enumerate the list it will just check internally if the list is null and in the case it isn’t it will return the element at the specified index like so list[index]. This is pretty lightweight so your problem lies in the source of the Add operation.

The Add operation first checks internally if its current capacity is big enough so that the new element can be added (it has more places reserved in memory than the amount of element it contains + 1). If it can be added it simply does so via the statement _items[_size++] = item;

In the case that not enough size was allocated previously the Add operation creates a new array with the new size which is needed. Afterwards the elements from the previously too small array need to be copied to the new array. This happens via the Array.Copy operation. Afterwards the internally held count of elements is incremented and then the element itself gets added.

As you can see there is pretty much going on behind the scenes. Personally I don’t know where the problem really originates. My best guess is that the memory in the internal array is already allocated but the value isn’t set while you access it with the ElementAt operation.

And such problems are why you need to manage the access to shared resources (resources which can be shared across threads). There are several techniques for this like locks, monitors, semaphores, …

You could also change the type of the collection to a thread-safe collection which takes care of the synchronisation issues itself.

2

solved Why there are “null” in c#? [closed]