A method accepts about 7 string parameters, and they need to be checked for null The basic way to do for all of them is simply !string.IsNullOrWhitespace(param1) and likewise Is there any better or smarter way ? Also, I may wrap the parameters in an object if that helps !
5 Answers
In case any null will be a false for you:
bool CheckAnyNull(params string[] strs)
{
foreach (var str in strs)
{
if (strs.IsNullOrWhiteSpace(str))
return true;
}
return false;
}
If you want to check if they are all null:
bool CheckAllNull(params string[] strs)
{
foreach (var str in strs)
{
if (!strs.IsNullOrWhiteSpace(str))
return false;
}
return true;
}
If you want a specific amount of null strings:
int CheckNullCount(params string[] strs)
{
int count;
foreach (var str in strs)
{
if (strs.IsNullOrWhiteSpace(str))
count++;
}
return count;
}
All of them can then be called like this:
CheckAnyNulls(str1, str2, str3, str4, str5, str6, str7);
CheckAllNulls(str1, str2, str3, str4, str5, str6, str7);
int nullStrs = CheckNullCount(str1, str2, str3, str4, str5, str6, str7);
or:
var strs = new string[7];
// populate the strs array.
CheckAnyNulls(strs);
CheckAllNulls(strs);
int nullStrs = CheckNullCount(strs);
Comments
If you are looking to return whatever one of the strings isn't empty, than this is a nice solution:
public static bool AreAllStringsEmpty(params string[] array)
{
return array.All(str => String.IsNullOrWhiteSpace(str));
}
public static bool AreAllStringsFull(params string[] array)
{
return array.All(str => !String.IsNullOrWhiteSpace(str));
}
public static bool AreAtleastOneStringFull(params string[] array)
{
return array.Any(str => !String.IsNullOrWhiteSpace(str));
}
And than use it like this:
AreAllStringsFull(param1, param2, ...);
Remark: You can add extension method to simplify the process, but it's still boilerplate code.
1 Comment
If you don't need to check for whitespace just compare to null:
if (param1 != null)
Conventionally, if the input is required to be not null you would throw an ArgumentNullException:
if (param1 == null)
throw new ArgumentNullException("param1");
Comments
You can build an extension method using reflection to check your entire object at once. However, since this gets expensive, you can use a library such as FastMember to minimize the reflection cost on subsequent object checks.
using System.Linq;
using FastMember;
namespace Utilities
{
public static class CheckStringProperties
{
public static bool AreAnyStringPropertiesNull<T>(this T model) where T : class
{
var accessor = TypeAccessor.Create(model.GetType());
return AreAnyStringPropertiesNull(model, accessor);
}
public static bool AreAnyStringPropertiesNull<T>(this T model) where T : class
{
var accessor = TypeAccessor.Create(model.GetType());
return AreAllStringPropertiesNull(model, accessor);
}
private static bool AreAnyStringPropertiesNull<T>(T model, TypeAccessor accessor)
{
foreach (var strng in GetStringProperties(accessor))
{
if (string.IsNullOrWhiteSpace(strng))
return true;
}
return false;
}
private static bool AreAllStringPropertiesNull<T>(T model, TypeAccessor accessor)
{
foreach (var strng in GetStringProperties(accessor))
{
if (!string.IsNullOrWhiteSpace(strng))
return true;
}
return false;
}
private static List<Member> GetStringProperties(TypeAccessor accessor)
{
return accessor.GetMembers().Where(x => x.Type == typeof(string)).ToList();
}
}
}
You can then just call the extension method on your class.
var containsNulls = classInstance.AreAnyStringPropertiesNull();
var areAllNull = classInstance.AreAllStringPropertiesNull();
str.Length == 0is the fastest way to check nullnull, as that will throw aNullReferenceExceptionif it isnull.NullReferenceExceptionif the string isnull