Here is an approach you can use that uses a collection of a simple data structure to hold your Old and Current level values:
class Level
{
public string Current { get; set; }
public string Old { get; set; }
}
class Program
{
static bool CompareLevels(ICollection<Level> levels)
{
foreach (var item in levels)
{
// Either of any level is HUB => false
if ((item.Current == "HUB") || (item.Old == "HUB"))
return false;
// Any Old level == any Current level => false
if (levels.Any(level => level.Old == item.Current))
return false;
}
// All levels met the criteria (no exceptions/failures)
return true;
}
// A simple console program to exercise the above function:
static void Main(string[] args)
{
// Should return false if any Old or Current levels are HUB
var levels = new List<Level>
{
new Level { Current = "HUB", Old = "ABCDEF" },
new Level { Current = "DEFGHI", Old = "HUB" }
};
Console.WriteLine(CompareLevels(levels));
// Should return true if all Old levels != any Current level and none are "HUB"
levels = new List<Level>
{
new Level { Current = "ABC123", Old = "ABCDEF" },
new Level { Current = "DEFGHI", Old = "DEF456" }
};
Console.WriteLine(CompareLevels(levels));
// Should return false if any Old level == any Current level
levels = new List<Level>
{
new Level { Current = "ABC123", Old = "ABCDEF" },
new Level { Current = "DEFGHI", Old = "ABC123" }
};
Console.WriteLine(CompareLevels(levels));
Console.ReadLine();
}
}
The iteration of the collection of levels will terminate at the first failure condition so will not wastefully iterate over the whole collection. The only condition under which the collection is completely enumerated is when there are no failures.
This may not be the most efficient but optimizations here will be vanishingly small for the numbers of levels you indicate are involved. It also requires that you initialise a collection to hold the levels to be compared.
It does however have the advantage of accommodating any number of "levels" and (imho) making the comparison criteria very clear and easily understood.
NOTE: In C# there is no problem using ==
to compare string values, other than when needing to take into account culture and case variations etc. ==
for strings will compare the value of the strings involved.