Check if any of class properties is not null/empty/was assigned I have a class that I use to display on a web page fieldset. However, all of the properties of this class are optionally filled in by the user, which means all of them could be null/default. In this case I would like not to render the fieldset at all, instead of displaying an empty one.

I wrote a simple isEmpty() method that checks all property values using reflection, but I'm afraid this may become way too expansive.

public class User
{
    [Display(Name = "First Name:")]
    public string FirstName { get; set; }

    [Display(Name = "Last Name:")]
    public string LastName { get; set; }

    public bool isEmpty()
    {
        Type t = this.GetType();

        var properties = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

        foreach (var prop in properties)
        {
            var value = prop.GetValue(this, null);

            if (value != null &amp;&amp; (string)value != "")
            {
                return false;
            }
        }

        return true;
    }
}


The isEmpty(), of course, assumes all properties are strings.

What do you think? Will this become potentially expansive as the class grows?
Naming:

Don't use variable names such as t. They're not meaningful, not for you, not for others. In this case, use type.

Class names, public fields and method names use PascalCase and not camelCase. So, isEmpty will become IsEmpty. But then again, this is also not a meaningful name. Boolean methods or properties in C# will most likely start with Is or Has. An example for the name of your method would be HasAllEmptyProperties.

var keyword:

Use var to declare your variables instead of declaring them explicitly. The compiler will determine the type for you and your code looks cleaner.

The method:

Your code looks good. You could however make it cleaner by using LinQ. Loops won't be avoided but it looks cleaner. I'm not saying this is a perfect solution but here goes:

public bool HasAllEmptyProperties()
{
    var type = GetType();
    var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
    var hasProperty = properties.Select(x =&gt; x.GetValue(this, null))
                                .Any(y =&gt; y != null &amp;&amp; !String.IsNullOrWhiteSpace(y.ToString()));
    return !hasProperty;
}


I could also have used the All() method and positive comparison instead of Any(). But this is inefficient as all elements have to be checked against the condition whereas Any() will return as soon as an element satisfies the condition (like in your loop).

You can take the comparison code and throw it in an extension method for reusability. It also makes your method-code cleaner, for example:

public static class Extensions
{
    public static bool IsNullOrEmpty(this object obj)
    {
        return obj == null || String.IsNullOrWhiteSpace(obj.ToString());
    }
}

public bool HasAllEmptyProperties()
{
    var type = GetType();
    var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
    var hasProperty = properties.Select(x =&gt; x.GetValue(this, null))
                                .Any(x =&gt; !x.IsNullOrEmpty());
    return !hasProperty;
}


Since you only have two properties, performace won't be a problem. Even if you were to add a couple more properties. Hope this helps!