Generic List<T> as IEnumerable<object>

Posted by Avi on Stack Overflow See other posts from Stack Overflow or by Avi
Published on 2010-05-20T12:07:39Z Indexed on 2010/05/20 12:10 UTC
Read the original article Hit count: 441

I'm trying to do cast a List to an IEnumerable, so I can verify that different lists are not null or empty:

Suppose myList is a List < T > . Then in the caller code I wanted:

       Validator.VerifyNotNullOrEmpty(myList as IEnumerable<object>,
                                     @"myList",
                                     @"ClassName.MethodName");

The valdiating code would be:

     public static void VerifyNotNullOrEmpty(IEnumerable<object> theIEnumerable,
                                        string theIEnumerableName,
                                        string theVerifyingPosition)
    {
        string errMsg = theVerifyingPosition + " " + theIEnumerableName;
        if (theIEnumerable == null)
        {
            errMsg +=  @" is null";
            Debug.Assert(false);
            throw new ApplicationException(errMsg);

        }
        else if (theIEnumerable.Count() == 0)
        {
            errMsg +=  @" is empty";
            Debug.Assert(false);
            throw new ApplicationException(errMsg);

        }
    }

However, this doens't work. It compiles, but theIEnumerable is null! Why?

© Stack Overflow or respective owner

Related posts about casting

Related posts about listt