Enumerating through .Net enums

Posted on June 18, 2007

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 ,
     InterestOnly,
     MixOfBoth,
 }

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;
  calculator.doStuff();   
  Assert.AreEqual(0, calculator.MonthlyPayments);
}
Comments
  1. testJune 18, 2007 @ 03:02 PM
    hello mcfly