How to check if IEnumerable is null or empty?

I don't think this function exists, but you can always create a small helper function that does exactly what you said to accomplish the same thing, while reducing clutter in your if statements.

Sure you could write that: public static class Utils { public static bool IsAny(this IEnumerable data) { return data! = null && data.Any(); } } however, be cautious that not all sequences are repeatable; generally I prefer to only walk them once, just in case.

I would drop the this there – I consider extension methods which are assumed to be called on null as a sign of ugly design. – Mormegil Feb 18 at 22:49 @Mormegil Why? Extension methods finally give C# some capability to work with nulls, which other languages (like Ruby) completely take for granted.

– Matt Greer Feb 18 at 22:54 Why is this necessarily bad? As in this case, it is sometimes very handy since it lets you treat things more homogeneously and with fewer special cases. – Mr. Putty Feb 18 at 22:54 @Mormegil meh - I can't get excited about that.

As long as the intent is clear, etc. – Marc Gravell? Feb 18 at 22:55 I see nothing wrong with it, as long as there's no implication that it shouldn't be null. – Gabe Feb 18 at 23:00.

Public static bool IsNullOrEmpty(this IEnumerable enumerable) { return enumerable == null ||!enumerable.Any(); }.

If you are using foreach to walk the IEnumerable, you won't have to check, since the loop will not be executed at all if there are no items in the IEnumerable.

1 you still have to check if the object is null – vlad Feb 18 at 22:50.

There isn't a straight up function like String. IsNullorEmpty but you can do your own test. YOu can even make it a function in your own utililty class I guess.

List Ts =? ; if(null! = Ts && 0Count) { //Ts is not Null or Empty } an alternative to the .

Count is .Any().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions