Home | Resume | Blog Brian Ensink's Blog | Understanding the params keyword

Understanding the params keyword

by Brian Ensink 17. December 2008 03:18

Any C# program that deals with strings will almost certainly make heavy use of the System.String.Format() function.  Before writing C# code I wrote C/C++ and MFC code and used both sprintf and CStringT::Format functions.  String.Format() seemed natural to me and I happily passed a variable number of arguments to the function without a second thought.

   1: String.Format("Hello, {0}", name);
   2: String.Format("x={0}, y={1}, z={2}", x, y, z);

But notice that the signature of the String.Format() function uses the params keyword to support a variable length argument list.  This keyword lets us pass zero or more objects to the function separated by commas.  Unfortunately MSDN does not clearly show the params keyword is used but Intellisense does as well as the Object Browser window inside Visual Studio and the very old .Net 1.1 documentation online.  Here is the actual signature:

   1: public static string Format(string format, params object[] args)
 
image

Its possible to write code to create an array of objects and pass that array to String.Format() instead.  In fact I have written code like that to call String functions like Trim() or Split().

   1: string fullpath = @"c:\abc\xyz\";
   2: char[] trimChars = new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
   3: string s = fullpath.Trim(trimChars);

But that is a lot more code to write every time I need to trim some characters off a string or split a string into subparts.  Now that I understand the params keyword and realize how it is used in the signature of these other functions it is much easier to write the same code like this:

   1: string s = fullpath.Trim(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

Its possible to use the params keyword when defining your own functions.  In fact this post was inspired by a logging class I recently refactored.  This logging class had a number of functions overloads to write a single line of text to a log file.  There was one overload to accept zero arguments, another to accept 1 argument, another for 2, another for 3 etc.  What happens when you need 4 arguments?  Or 5?  Add more overloads.

I got rid of all the overloads and replaced it with a single function that used the params keyword similar to this one below.  Notice the striking similarity to the String.Format() function signature above.  This WriteLine function can accept a format string and zero or more arguments allowing it to replace every overload in the logging class.

   1: void WriteLine(string format, params object[] args)
   2: {
   3:     string text = string.Format(format, args);
   4:     ...
   5: }

If you do use the params keyword in your own function keep in mind two things.  You can only use it at most one time per function and no other arguments can come after it.  So if I wanted to add a flag to control whether the function should also write to the system event log that argument would have to come before the params argument.

   1: static void WriteLine(bool bEventLog, string format, params object[] args)
   2: {
   3:     string text = string.Format(format, args);
   4:     if (bEventLog)
   5:     {
   6:         ...
   7:     }
   8:     ...
   9: }

Tags:

Software Development

Comments are closed

About the author

I am currently a .NET developer and really enjoy the platform.  .NET seems to be able to take the developer whereever he/she wants to go.  To the desktop, to the web, to a database, etc.  At my day job I write desktop apps but I also like to toy with other tech as I have time.