Enumerating through .Net enums

So, I was working on something that required a loop through enumerated types (in this case, go through the different repayment methods of a mortgage calculator)

public enum RepaymentMethod
  RepaymentOnly ,

To loop through this, use Enum.GetValues() to grab an array of the enumerations, and walk through it like this:

Array repaymentMethods = Enum.GetValues(typeof(RepaymentMethod));
foreach (RepaymentMethod method in repaymentMethods)
  calculator.RepaymentMethod = method;
  Assert.AreEqual(0, calculator.MonthlyPayments);

Subscribe via RSS

Back to all blog posts