ParamArrayAttribute Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menunjukkan bahwa metode akan memungkinkan jumlah variabel argumen dalam pemanggilannya. Kelas ini tidak dapat diwariskan.
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
- Warisan
- Atribut
Contoh
Contoh berikut mendefinisikan Temperature kelas yang menyertakan Display metode, yang dimaksudkan untuk menampilkan satu atau beberapa nilai suhu yang diformat. Metode ini memiliki satu parameter, formats, yang didefinisikan sebagai array parameter.
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
Contoh berikut mengilustrasikan tiga panggilan berbeda ke metode .Temperature.Display Pada metode pertama, metode ini melewati array string format. Pada yang kedua, metode ini diteruskan empat string format individual sebagai argumen. Di yang ketiga, metode ini dipanggil tanpa argumen. Seperti yang diilustrasikan oleh output dari contoh, pengkompilasi Visual Basic dan C# menerjemahkannya ke dalam panggilan ke metode Display dengan array string kosong.
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
Keterangan
ParamArrayAttribute menunjukkan bahwa parameter metode adalah array parameter. Array parameter memungkinkan spesifikasi jumlah argumen yang tidak diketahui. Array parameter harus menjadi parameter terakhir dalam daftar parameter formal, dan harus berupa array dimensi tunggal. Ketika metode dipanggil, array parameter mengizinkan argumen ke metode yang akan ditentukan dengan salah satu dari dua cara:
Sebagai ekspresi tunggal dari jenis yang secara implisit dapat dikonversi ke jenis array parameter. Parameter array berfungsi sebagai parameter nilai.
Sebagai argumen nol atau lebih, di mana setiap argumen adalah ekspresi dari jenis yang secara implisit dapat dikonversi ke jenis elemen array parameter.
Contoh di bagian berikutnya menggambarkan kedua konvensi panggilan.
Note
Biasanya, ParamArrayAttribute tidak digunakan langsung dalam kode. Sebagai gantinya, kata kunci bahasa individual, seperti ParamArray di Visual Basic dan params di C#, digunakan sebagai pembungkus untuk kelas ParamArrayAttribute. Beberapa bahasa, seperti C#, bahkan mungkin memerlukan penggunaan kata kunci bahasa dan melarang penggunaan ParamArrayAttribute.
Selama resolusi kelebihan beban, ketika pengkompilasi yang mendukung array parameter mengalami kelebihan beban metode yang tidak ada tetapi memiliki satu parameter yang lebih sedikit daripada kelebihan beban yang mencakup array parameter, mereka akan mengganti metode dengan kelebihan beban yang mencakup array parameter. Misalnya, panggilan ke String.Split() metode instans (yang tidak ada di String kelas) diselesaikan sebagai panggilan ke String.Split(Char[]) metode . Pengkompilasi juga akan meneruskan array kosong dari jenis yang diperlukan ke metode . Ini berarti bahwa metode harus selalu disiapkan untuk menangani array yang panjangnya nol ketika memproses elemen dalam array parameter. Contoh memberikan ilustrasi.
Untuk informasi selengkapnya tentang menggunakan atribut, lihat Atribut.
Konstruktor
| Nama | Deskripsi |
|---|---|
| ParamArrayAttribute() |
Menginisialisasi instans ParamArrayAttribute baru kelas dengan properti default. |
Properti
| Nama | Deskripsi |
|---|---|
| TypeId |
Ketika diimplementasikan dalam kelas turunan, mendapatkan pengidentifikasi unik untuk Attributeini. (Diperoleh dari Attribute) |
Metode
| Nama | Deskripsi |
|---|---|
| Equals(Object) |
Mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu. (Diperoleh dari Attribute) |
| GetHashCode() |
Mengembalikan kode hash untuk instans ini. (Diperoleh dari Attribute) |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| IsDefaultAttribute() |
Ketika ditimpa dalam kelas turunan, menunjukkan apakah nilai instans ini adalah nilai default untuk kelas turunan. (Diperoleh dari Attribute) |
| Match(Object) |
Saat ditimpa dalam kelas turunan, mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu. (Diperoleh dari Attribute) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |
Implementasi Antarmuka Eksplisit
| Nama | Deskripsi |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Memetakan sekumpulan nama ke sekumpulan pengidentifikasi pengiriman yang sesuai. (Diperoleh dari Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Mengambil informasi jenis untuk objek, yang dapat digunakan untuk mendapatkan informasi jenis untuk antarmuka. (Diperoleh dari Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Mengambil jumlah antarmuka informasi jenis yang disediakan objek (baik 0 atau 1). (Diperoleh dari Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Menyediakan akses ke properti dan metode yang diekspos oleh objek. (Diperoleh dari Attribute) |