开发者

ReSharper: Possible NullReferenceException with Enumerator?

开发者 https://www.devze.com 2023-03-05 02:01 出处:网络
ReSharper notifies me about a possible System.NullReferenceException for the following code: IEnumerator<IEdgeData> edgeEnumerator = edgeData.GetEnumerator();

ReSharper notifies me about a possible System.NullReferenceException for the following code:

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

Specifically, it underlines:

edgeEnumerator.Current

I cannot figure out under which circumstanc开发者_开发百科es the exception could occur. My understanding is that the while-loops inner statements will only be executed if MoveNext() can set the enumerator on the next element.


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

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号