dev hedgehog
dev hedgehog

Reputation: 8791

Check for null inside null-coalescing operator

I am using Resharper and usually that thingy underlines me parts of code which might throw null reference exception. But in this case Resharper seems to think the code is fine. Why?

public static bool Check<T>(IEnumerable<T> docs)
{
   var list = docs as IList<T> ?? docs.ToList();
   return list.Count == 3;
}

Resharper is not underlining me anything here. What am I missing here? There should be an exception if docs is null. Thanks in advance guys.

Upvotes: 0

Views: 113

Answers (1)

Rodders
Rodders

Reputation: 2435

I think ReSharper checks for NullReferenceExceptions but IEnumerable.ToList() throws an ArgumentNullException.

It makes sense. You may pass in an object that implements the null pattern which ReSharper probably wouldn't know about, this could result in wrong and annoying warnings so I assume that this would be difficult to implement in ReSharper

Upvotes: 1

Related Questions