ParamArrayAttribute 類別

定義

表示方法將允許在引動過程中使用不定數目的引數。Indicates that a method will allow a variable number of arguments in its invocation. 此類別無法獲得繼承。This class cannot be inherited.

public ref class ParamArrayAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Parameter, AllowMultiple=false, Inherited=true)]
public sealed class ParamArrayAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Parameter, AllowMultiple=false, Inherited=true)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ParamArrayAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Parameter, AllowMultiple=false, Inherited=true)>]
type ParamArrayAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Parameter, AllowMultiple=false, Inherited=true)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ParamArrayAttribute = class
    inherit Attribute
Public NotInheritable Class ParamArrayAttribute
Inherits Attribute
繼承
ParamArrayAttribute
屬性

範例

下列範例 Temperature 會定義包含方法的類別 Display ,這是用來顯示一或多個格式化的溫度值。The following example defines a Temperature class that includes a Display method, which is intended to display one or more formatted temperature values. 方法具有單一參數, formats 其定義為參數陣列。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(params string []formats)
   {
      if (formats.Length == 0)
      {
         Console.WriteLine(this.ToString("G"));
      }
      else
      {
         foreach (string format in formats)
         {
            try {
               Console.WriteLine(this.ToString(format));
            }
            // If there is an exception, do nothing.
            catch { }
         }
      }
   }
}
Public Class Temperature 
   Private temp As Decimal
   
   Public Sub New(temperature As Decimal)
      Me.temp = temperature
   End Sub
   
   Public Overrides Function ToString() As String
      Return ToString("C")
   End Function
   
   Public Overloads Function ToString(format As String) As String
      If String.IsNullOrEmpty(format) Then format = "G"
      
      Select Case format
         Case "G", "C"
            Return temp.ToString("N") + "  °C"
         Case "F"
            Return (9 * temp / 5 + 32).ToString("N") + "  °F"
         Case "K" 
            Return (temp + 273.15d).ToString("N") + "  °K" 
         Case Else
            Throw New FormatException(String.Format("The '{0}' format specifier is not supported", _
                                                    format))
      End Select                                                         
   End Function         
   
   Public Sub Display(<[ParamArray]()> formats() As String)
      If formats.Length = 0 Then
         Console.WriteLine(Me.ToString("G"))
      Else   
         For Each format As String In formats
            Try
               Console.WriteLine(Me.ToString(format))
            ' If there is an exception, do nothing.
            Catch
            End Try   
         Next
      End If
   End Sub
End Class

下列範例說明三種不同的方法呼叫 Temperature.DisplayThe 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. 如範例的輸出所示,Visual Basic 和 c # 編譯器會將其轉譯為 Display 具有空字串陣列的方法呼叫。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 Class1
{
   public static void Main()
   {
      Temperature temp1 = new Temperature(100);
      string[] formats = { "C", "G", "F", "K" };

      // Call Display method with a string array.
      Console.WriteLine("Calling Display with a string array:");
      temp1.Display(formats);
      Console.WriteLine();

      // Call Display method with individual string arguments.
      Console.WriteLine("Calling Display with individual arguments:");
      temp1.Display("C", "F", "K", "G");
      Console.WriteLine();

      // Call parameterless Display method.
      Console.WriteLine("Calling Display with an implicit parameter array:");
      temp1.Display();
   }
}
// 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
Public Module Example
   Public Sub Main()
      Dim temp1 As New Temperature(100)
      Dim formats() As String = { "C", "G", "F", "K" } 

      ' Call Display method with a string array.
      Console.WriteLine("Calling Display with a string array:")
      temp1.Display(formats)
      Console.WriteLine()
      
      ' Call Display method with individual string arguments.
      Console.WriteLine("Calling Display with individual arguments:")
      temp1.Display("C", "F", "K", "G")
      Console.WriteLine()
      
      ' Call parameterless Display method.
      Console.WriteLine("Calling Display with an implicit parameter array:")
      temp1.Display()
   End Sub
End Module
' 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

備註

ParamArrayAttribute表示方法參數是參數陣列。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.

注意

一般而言, ParamArrayAttribute 不會直接在程式碼中使用。Typically, the ParamArrayAttribute is not used directly in code. 相反地,個別語言關鍵字(例如 ParamArray Visual Basic 和 params c # 中的)會用來做為類別的包裝函式 ParamArrayAttributeInstead, individual language keywords, such as ParamArray in Visual Basic and params in C#, are used as wrappers for the ParamArrayAttribute class. 某些語言(例如 c #)甚至可能需要使用 language 關鍵字,並禁止使用 ParamArrayAttributeSome 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. 例如,呼叫不 String.Split() 存在於類別中的實例方法 (, String) 會被解析為方法的呼叫 String.Split(Char[])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.

如需使用屬性的詳細資訊,請參閱 屬性For more information about using attributes, see Attributes.

建構函式

ParamArrayAttribute()

使用預設屬性,初始化 ParamArrayAttribute 類別的新執行個體。Initializes a new instance of the ParamArrayAttribute class with default properties.

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。When implemented in a derived class, gets a unique identifier for this Attribute.

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。Returns a value that indicates whether this instance is equal to a specified object.

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。Returns the hash code for this instance.

(繼承來源 Attribute)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can be used to get the type information for an interface.

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

(繼承來源 Attribute)

適用於

另請參閱