Tal Tal - 2 years ago 96
C# Question

Check if all the properties of class are null

I have the following code that should check if all the properties of class are null. I tried the code below but it didn't work. Why?
enter image description here

Answer Source

You could make a property IsInitialized, that does this internally:

public bool IsInitialized
        return this.CellPhone == null && this.Email == null && ...;

Then just check the property IsInitialized:

if (myUser == null || myUser.IsInitialized)
{ ... }

Another option is the use of reflection to walk over and check all properties, but it seems overkill to me. Also, this gives you the freedom to deviate from the original design (when you choose all properties, except one should be null for example).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download