Optional parameter demonstration with method Power. - CSharp Custom Type

CSharp examples for Custom Type:Method Parameter

Description

Optional parameter demonstration with method Power.

Demo Code

using System;//from  w  w w . ja va2 s . com
class CalculatePowers
{
   static void Main()
   {
      Console.WriteLine($"Power(10) = {Power(10)}");
      Console.WriteLine($"Power(2, 10) = {Power(2, 10)}");
   }
   // use iteration to calculate power
   static int Power(int baseValue, int exponentValue = 2)
   {
      int result = 1;
      for (int i = 1; i <= exponentValue; ++i)
      {
         result *= baseValue;
      }
      return result;
   }
}

Result


Related Tutorials