mardi 19 février 2019

How to check more than 1 property of object have value

I have a class with n properties, and I need to know if more than 1 property has value, it will be false, I write a method like this CheckValue

public class ClassExample
    {
        public string Value1 { get; set; }
        public string Value2 { get; set; }
        public string Value3 { get; set; }

        public bool CheckValue()
        {
            if ((Value1 != null ? 1 : 0) +
                (Value2 != null ? 1 : 0) +
                (Value3 != null ? 1 : 0) > 1)
            {
                return true;
            }

            return false;
        }
    }

Are there any better way to write CheckValue method?

Aucun commentaire:

Enregistrer un commentaire