vendredi 7 septembre 2018

Efficient way of Comparing Counts of 3 different lists

I have 3 list objects and I need them to all have the same count.. or all be empty (Count = 0).

If one or more lists has a larger/smaller count than the other list(s) then I need to catch that.

Is there a more efficient way of writing this then doing multiple if statements?

public static bool ThreeListComparison(List<string> lstOne,
    List<int> lstTwo, List<decimal> lstThree)
{
    var firstLstCount = lstOne.Count;
    var secondLstCount = lstTwo.Count;
    var thirdLstCount = lstThree.Count;

    if ((firstLstCount == 0 || secondLstCount == 0 || thirdLstCount == 0) && (firstLstCount != 0 || secondLstCount == 0) &&
        (firstLstCount == 0 || secondLstCount != 0)) return true;

    if (firstLstCount == 0 && secondLstCount != 0) return false;

    if (firstLstCount != 0 && secondLstCount == 0) return false;

    if (firstLstCount == 0 || secondLstCount == 0) return true;

    return firstLstCount == secondLstCount;
}

This is what I've started with two lists, but after writing it I am hoping for a better way.

Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire