Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

ParamArrayAttribute Class

Indicates that a method will allow a variable number of arguments in its invocation. This class cannot be inherited.

System.Object
  System.Attribute
    System.ParamArrayAttribute

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = true, AllowMultiple = false)]
[ComVisibleAttribute(true)]
public sealed class ParamArrayAttribute : Attribute

The ParamArrayAttribute type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ParamArrayAttributeInitializes a new instance of the ParamArrayAttribute class with default properties.
Top
  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360EqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The ParamArrayAttribute indicates that a method parameter is a parameter array. A parameter array allows the specification of an unknown number of arguments. A parameter array must be the last parameter in a formal parameter list, and it must be a single-dimension array. When the method is called, a parameter array permits arguments to a method to be specified in either of two ways:

  • As a single expression of a type that is implicitly convertible to the parameter array type. The parameter array functions as a value parameter.

  • As zero or more arguments, where each argument is an expression of a type that is implicitly convertible to the type of the parameter array element.

The example in the next section illustrates both calling conventions.

NoteNote:

Typically, the ParamArrayAttribute is not used directly in code. Instead, individual language keywords, such as ParamArray in Visual Basic and params in C#, are used as wrappers for the ParamArrayAttribute class. Some languages, such as C#, may even require the use of the language keyword and prohibit the use of ParamArrayAttribute.

During overload resolution, when compilers that support parameter arrays encounter a method overload that does not exist but has one fewer parameter than an overload that includes a parameter array, they will replace the method with the overload that includes the parameter array. For example, a call to the String.Split() instance method (which does not exist in the String class) is resolved as a call to the String.Split(Char[]) method. The compiler will also pass an empty array of the required type to the method. This means that the method must always be prepared to handle an array whose length is zero when it processes the elements in the parameter array. The example provides an illustration.

The following example defines a Temperature class that includes a Display method, which is intended to display one or more formatted temperature values. The method has a single parameter, formats, which is defined as a parameter array.


using System;

public class Temperature
{
   private decimal temp;

   public Temperature(decimal temperature)
   {
      this.temp = temperature;
   }

   public override string ToString()
   {
      return ToString("C");
   }

   public string ToString(string format)
   {
      if (String.IsNullOrEmpty(format))
         format = "G";

      switch (format.ToUpper())
      {
         case "G":
         case "C":
            return temp.ToString("N") + "  °C";
         case "F":
            return (9 * temp / 5 + 32).ToString("N") + "  °F";
         case "K":
            return (temp + 273.15m).ToString("N") + "  °K";
         default:
            throw new FormatException(String.Format("The '{0}' format specifier is not supported",
                                                    format));
      }
   }

   public void Display(System.Windows.Controls.TextBlock outputBlock, 
                       params string[] formats)
   {
      if (formats.Length == 0)
      {
         outputBlock.Text += this.ToString("G") + "\n";
      }
      else
      {
         foreach (string format in formats)
         {
            try
            {
               outputBlock.Text += this.ToString(format) + "\n";
            }
            // If there is an exception, do nothing.
            catch { }
         }
      }
   }
}


The following example illustrates three different calls to the Temperature.Display method. In the first, the method is passed an array of format strings. In the second, the method is passed four individual format strings as arguments. In the third, the method is called with no arguments. As the output from the example illustrates, the Visual Basic and C# compilers translate this into a call to the Display method with an empty string array.


public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Temperature temp1 = new Temperature(100);
      string[] formats = { "C", "G", "F", "K" };

      // Call Display method with a string array.
      outputBlock.Text += "Calling Display with a string array:" + "\n";
      temp1.Display(outputBlock, formats);
      outputBlock.Text += "\n";

      // Call Display method with individual string arguments.
      outputBlock.Text += "Calling Display with individual arguments:" + "\n";
      temp1.Display(outputBlock, "C", "F", "K", "G");
      outputBlock.Text += "\n";

      // Call Display method with implicit parameter array.
      outputBlock.Text += "Calling Display with an implicit parameter array:" + "\n";
      temp1.Display(outputBlock);
   }
}
// The example displays the following output:
//       Calling Display with a string array:
//       100.00  °C
//       100.00  °C
//       212.00  °F
//       373.15  °K
//       
//       Calling Display with individual arguments:
//       100.00  °C
//       212.00  °F
//       373.15  °K
//       100.00  °C
//       
//       Calling Display with an implicit parameter array:
//       100.00  °C


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.