Professor Mustard Programming
 

Programming 101 - Unit 03 - Modularity

03e) Params Keyword
 
The "params" keyword is a pretty brief topic, but it's so cool (and yet so over-looked) that I wanted to make a special point of showing it to you. Consider the following method:

static void PrintSum(params int[] nums)
{
  int ans = 0;

  foreach(int num in nums)
    ans += num;

  Console.WriteLine("The answer is " + ans.ToString());
}



Obviously, it accepts an integer array, adds up the elements and then prints the result to the console. But what does that "params" keyword mean? Well, it means that when we actually call the method, we can do this:

PrintSum(7, 2, 5, 8);


Yeah, I know, the method only takes one parameter, an integer array. But because we've used the "params" keyword on that parameter, we can pass individual integer values into the method as separate parameters, and C# will build them into an array for us by the time they reach the method! Without the params keyword, we'd have to do this:

PrintSum(new int[] {7, 2, 5, 8});


Isn't that ugly? Of course it is. The params keyword is really handy when you don't want to screw around building a new array that you only need for a single method call.

There's only two stipulations. First, and perhaps most obvious, is that you can only apply the "params" keyword to a one-dimensional array of any type. Second, if the method contains multiple parameters, the one flagged as "param" must be listed last. Otherwise, C# could have trouble figuring out where the params argument ends and the next "real" argument begins.

Just as a cautionary note, be mindful of method overloads when using the params keyword. For example, in the following code, which method overload do you think will actually be called?

DoStuff("hi");

private void DoStuff(string hi)
{
  con.WriteLine("1");
}

private void DoStuff(params string[] hi)
{
  con.WriteLine("2");
}



It so happens that, if you only provide one string argument, C# will assume that you wanted to call the method which accepts a simple string. But if you were to delete that simpler method, the method which accepts a params string[] would work, too... so they both technically match! That is the one drawback of the params keyword... it can add a little bit of confusion when you get into method overloads. Just be aware of it!

Anyways, that's your lesson on params! The next time you write a method that accepts an array (especially an array of simple datatypes), see if using the params keyword makes sense... if so, you can make life a little easier for whoever needs to call that method!



using System;

namespace J_ParamsKeyword
{
  class Class1
  {
    static readonly string ln = Environment.NewLine;

    [STAThread]
    static void Main(string[] args)
    {
      // We can pass as many string parameters into "items" as we want
      // They will automatically be built into an array at the method's end

      PrintStrings("hi"); // one string
      Console.WriteLine();

      PrintStrings("no", "yes"); // two strings
      Console.WriteLine();

      PrintStrings("never", "eat", "shredded", "wheat"); // four strings
      Console.WriteLine();

      // Another example
      PrintSum(11, 43, 61, 23);
      PromptForExit();
    }

    static void PrintStrings(params string[] items)
    {
      foreach(string item in items)
        Console.Write(item + ", ");
    }

    static void PrintSum(params int[] nums)
    {
      int ans = 0;

      foreach(int num in nums)
        ans += num;

      Console.WriteLine("The answer is " + ans.ToString());
    }

    static void PromptForExit()
    {
      Console.Write(ln + "Program complete! Hit enter to exit...");
      Console.ReadLine();
    }
  }
}