jloga4
6/11/2017 - 2:40 PM

Example of how to use named arguments with methods. From the Microsoft C# Reference https://docs.microsoft.com/en-us/dotnet/csharp/programmi

Example of how to use named arguments with methods. From the Microsoft C# Reference https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments

static void Main(string[] args)
{
  // The method can be called in the normal way, by using positional arguments.
  Console.WriteLine(CalculateBMI(123, 64));

  // Named arguments can be supplied for the parameters in either order.
  Console.WriteLine(CalculateBMI(weight: 123, height: 64));
  Console.WriteLine(CalculateBMI(height: 64, weight: 123));

  // Positional arguments cannot follow named arguments.
  // The following statement causes a compiler error.
  //Console.WriteLine(CalculateBMI(weight: 123, 64));

  // Named arguments can follow positional arguments.
  Console.WriteLine(CalculateBMI(123, height: 64));
}

static int CalculateBMI(int weight, int height)
{
  return (weight * 703) / (height * height);
}

//  21
//  21
//  21
//  21