ReSharper: is it possible to throw a NullReferenceException using Enumerator?

ReSharper notifies me of the possible System.NullReferenceExceptionfor the following code:

 IEnumerator<IEdgeData> edgeEnumerator = edgeData.GetEnumerator();
 while (edgeEnumerator.MoveNext())
 {
    ConvId fromConvId = edgeEnumerator.Current.From;
    ...
 }

In particular, this emphasizes:

edgeEnumerator.Current

I cannot understand under what circumstances an exception can occur. I understand that the internal while-loops statements will only be executed if they MoveNext()can set an enumerator for the next element.

+3
source share
1 answer

The next element may be null. For example, the following code:

new List<SampleClass> { null, null, null }

still give you an enumeration for each element, but the element itself is null.

+5
source

All Articles