params :
public static void ThrowIfNull(params object[] input)
{
foreach (var item in input)
{
throw new NullReferenceException();
}
}
:
int? nullableInt = null;
string someNullString = null;
ThrowIfNull(nullableInt, someNullString);
. , IEnumerable:
public static class NullExtensionMethods
{
public static void ThrowIfHasNull<T>(this IEnumerable collection)
where T : Exception, new()
{
foreach (var item in collection)
{
if (item == null)
{
throw new T();
}
}
}
public static void ThrowIfHasNull(this IEnumerable collection)
{
ThrowIfHasNull<NullReferenceException>(collection);
}
}
:
string someNullString = null;
new string[] { someNullString }.ThrowIfHasNull();
new string[] { someNullString }.ThrowIfHasNull<ArgumentNullException>();
, . :
public static class NullExtensionMethods
{
public static bool HasNull(this IEnumerable collection)
{
foreach (var item in collection)
{
if (item == null)
{
return true;
}
}
return false;
}
}
:
var nullDetected = new string[] { someNullString }.HasNull();
, , . , , String.IsNullOrEmpty. HasNullOrEmpty:
public static bool HasNullOrEmpty(this IEnumerable<string> collection)
{
foreach (var item in collection)
{
if (string.IsNullOrEmpty(item))
{
return true;
}
}
return false;
}
, IEnumerable<string>