BUG: doesn't recognize custom IsNullOrEmpty extension method

We have a generic extension method that we use for null checks. We still receive the "Possible System.NullReferenceException" when the extension method is used.

Extension Method:

    public static bool IsNullOrEmpty<T>(this IEnumerable<T> target)
    {
        bool flag = true;
        if (target != null)
        {
            using (IEnumerator<T> enumerator = target.GetEnumerator())
            {
                if (enumerator.MoveNext())
                {
                    T current = enumerator.Current;
                    flag = false;
                }
            }
        }
        return flag;
    }

2 comments
Comment actions Permalink

Hello!

 

Thank you for the feedback.

Could you please provide also code sample demonstrating "Possible System.NullReferenceException" when the extension method is used?

Thanks in advance.

0
Comment actions Permalink

I suspect you'll need to add the ContractAnnotation attribute to the method.

[ContractAnnotation("null => true")]
public static bool IsNullOrEmpty<T>(this IEnumerable<T> target)
0

Please sign in to leave a comment.