2

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 !

7
  • if you think about performance issue then, str.Length == 0 is the fastest way to check null Commented Jun 2, 2015 at 17:34
  • 2
    @gypsyCoder That's the fastest way to check for an empty string, not null, as that will throw a NullReferenceException if it is null. Commented Jun 2, 2015 at 17:35
  • Won't that crash if str is null? Commented Jun 2, 2015 at 17:35
  • 1
    @gypsyCoder, that will throw a NullReferenceException if the string is null Commented Jun 2, 2015 at 17:35
  • What about adding all strings together to one string and then check if its length is shorter then 7 or check for null? Commented Jun 2, 2015 at 17:38

5 Answers 5

6

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);
Sign up to request clarification or add additional context in comments.

Comments

1

You can pass the string into list and then check like this:

if(List.All(x=>string.IsNullOrWhiteSpace(x)))
{

}

Comments

1

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

This only works if they are all null/empty. If he wants to check them individually so that they must all have something, this will fail. (str1 and str2 = empty, strN = something then the check passes)
0

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

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.