ParamArrayAttribute 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示方法會在其調用中允許變數數目的自變數。 此類別無法獲得繼承。
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
- 繼承
- 屬性
範例
以下範例定義了一個 Temperature 包含方法 Display 的類別,該方法旨在顯示一個或多個格式化的溫度值。 此方法有一個單一參數, formats定義為參數陣列。
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 { }
}
}
}
}
open System
type Temperature(temperature) =
override this.ToString() =
this.ToString "C"
member _.ToString(format) =
let format =
if String.IsNullOrEmpty format then "G"
else format
match format.ToUpper() with
| "G" | "C" ->
$"{temperature:N} °C"
| "F" ->
$"{9. * temperature / 5. + 32.:N} °F"
| "K" ->
$"{temperature + 273.15:N} °K"
| _ ->
raise (FormatException $"The '{format}' format specifier is not supported")
member this.Display([<ParamArray>]formats: string[]) =
if formats.Length = 0 then
printfn $"""{this.ToString "G"}"""
else
for format in formats do
try
printfn $"{this.ToString format}"
// If there is an exception, do nothing.
with _ -> ()
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.Display 法。 在第一種中,方法會傳遞一組格式字串陣列。 在第二種情況下,方法會被傳遞四個獨立格式字串作為參數。 第三種情況下,該方法不需參數。 如範例所示,Visual Basic 與 C# 編譯器會將此轉換成對 Display 方法的呼叫,並使用空字串陣列。
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
let temp1 = Temperature 100.
let formats = [| "C"; "G"; "F"; "K" |]
// Call Display method with a string array.
printfn "Calling Display with a string array:"
temp1.Display formats
// Call Display method with individual string arguments.
printfn "\nCalling Display with individual arguments:"
temp1.Display("C", "F", "K", "G")
// Call parameterless Display method.
printfn "\nCalling 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 方法參數是參數陣列。 參數陣列允許指定未知數量的參數。 參數陣列必須是正式參數列表中的最後一個參數,且必須是單維陣列。 當呼叫該方法時,參數陣列允許以兩種方式指定方法的參數:
作為一個可隱含可轉換為參數陣列型別的單一表達式。 參數陣列作為值參數運作。
作為零個或多個參數,其中每個參數是一個可隱含轉換為參數陣列元素類型的表達式。
下一節的範例說明了兩種呼叫慣例。
Note
通常,這些 ParamArrayAttribute 不會直接用於程式碼中。 取而代之的是,個別語言關鍵字,如 Visual Basic 中的 ParamArray 和 C# 中的 params,作為 ParamArrayAttribute 類別的包裝器。 某些語言,如 C#,甚至可能要求使用語言關鍵字,並禁止使用 ParamArrayAttribute。
在超載解析過程中,當支援參數陣列的編譯器遇到不存在但參數比包含參數陣列的超載少一個參數的方法時,會用包含參數陣列的超載方法替換該方法。 例如,對 String.Split() 實例方法的呼叫(該類別中不存在 String )會被解析為對 String.Split(Char[]) 該方法的呼叫。 編譯器也會將所需型別的空陣列傳遞給該方法。 這表示該方法必須始終準備好處理長度為零的陣列,當它處理參數陣列中的元素時。 此範例提供圖例。
欲了解更多屬性的使用資訊,請參閱屬性。
建構函式
| 名稱 | Description |
|---|---|
| ParamArrayAttribute() |
初始化一個帶有預設屬性的新 ParamArrayAttribute 類別實例。 |
屬性
| 名稱 | Description |
|---|---|
| TypeId |
在衍生類別中實作時,取得這個 Attribute的唯一標識碼。 (繼承來源 Attribute) |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
傳回值,這個值表示這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| GetHashCode() |
傳回這個實例的哈希碼。 (繼承來源 Attribute) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsDefaultAttribute() |
在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。 (繼承來源 Attribute) |
| Match(Object) |
在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。 (繼承來源 Attribute) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。 (繼承來源 Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
擷取 物件的型別資訊,可用來取得介面的類型資訊。 (繼承來源 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
擷取物件提供的類型資訊介面數目 (0 或 1)。 (繼承來源 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
提供物件所公開屬性和方法的存取權。 (繼承來源 Attribute) |