ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

AsNullSafeEnumerable

You don't need check whether the collection is null.

Source

public static System.Collections.Generic.IEnumerable<T> AsNullSafeEnumerable<T>(this System.Collections.Generic.IEnumerable<T> collection) {
	if (collection.IsNotNullNorZero()) {
		return collection;
	} else {
		return new T[] { };
	}
}

Example

string[] names = null;
foreach (var name in names.AsNullSafeEnumerable()) {
	Console.WriteLine("Hello, {0}", name);
}

Author: Rrr

Submitted on: 31 mrt. 2011

Language: C#

Type: System.Collections.Generic.IEnumerable<T>

Views: 4403