2

I have an object with multiple strings.
Is there a way to check if one of the values is null or all values are set?
Or do I have to do it like this:

if (!string.IsNullOrWhiteSpace(object.string1) || !string.IsNullOrWhiteSpace(object.string2) || !string.IsNullOrWhiteSpace(object.string3))
{

}
4
  • 1
    Well, if you have only 3 properties you can keep this, else you may get help from reflection. Commented Feb 15, 2022 at 7:11
  • 1
    Is the object's state invalid if one of them is not set? Should all three be set at all times of the lifetime of that object? Are the strings (fields) mutable? Commented Feb 15, 2022 at 8:27
  • 1
    @Fildor the fields are mutable. I create an empty object and before I want to add this object to a list, I want to make sure, that all required fields have values Commented Feb 15, 2022 at 8:55
  • 1
    Your condition will be true if any one of the items is non-null/whitespace, even if one or more is null/whitespace. Is that what you intended? Commented Feb 15, 2022 at 9:09

4 Answers 4

2

You can gather all your strings into an array and then run .Any() method:

if (new[] { obj.string1, obj.string2, obj.string3 }.Any(string.IsNullOrWhiteSpace))
{
    
}

Alternatively you can use reflection (which will affect your code's performance), to scan all the strings of your object and check your condition:

var anyEmpty = obj.GetType().GetProperties()
    .Any(x => x.PropertyType == typeof(string)
              && string.IsNullOrWhiteSpace(x.GetValue(obj) as string));
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a for loop to iterate trough all strings and check if they are blank or empty.

EDIT: You probably have to add all the strings into an array or a list, because they all have different names like string1, string2 and string3

Comments

1

If you do this a lot, you could write a method to check it:

public static class Ensure
{
    public static bool NoneNullOrWhitespace(params string?[] items)
    {
        return !items.Any(string.IsNullOrWhiteSpace);
    }
}

Which for your case you would call like this:

if (Ensure.NoneNullOrWhitespace(object.string1, object.string2, object.string3))
{
    ...
}

Comments

1

If it's an option for you to define a class for your objects, you could let the class itself handle the "all strings not null or whitespace"-check:

public class MyObject
{
    public string String1 { get; set; }
    public string String2 { get; set; }
    public string String3 { get; set; }

    public bool StringsAreNotNullOrWhiteSpace => !Strings.Any(string.IsNullOrWhiteSpace);

    private string[] Strings => new[] { String1, String2, String3 };
}

and use it like this:

var myObject = new MyObject();
//Populate myObject

if (myObject.StringsAreNotNullOrWhiteSpace)
{
    //Add myObject to list
}

(The implementation of StringsAreNotNullOrWhiteSpace is basically what @mickl did in their first suggestion, but returning the opposite bool value.)

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.