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
- 상속
- 특성
예제
다음 예제에서는 하나 이상의 형식이 지정된 온도 값을 표시하기 위한 메서드를 포함하는 Display 클래스를 정의 Temperature 합니다. 메서드에는 매개 변수 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 매개 변수가 매개 변수 배열임을 나타냅니다. 매개 변수 배열을 사용하면 알 수 없는 수의 인수를 지정할 수 있습니다. 매개 변수 배열은 정식 매개 변수 목록의 마지막 매개 변수여야 하며 1차원 배열이어야 합니다. 메서드가 호출될 때 매개 변수 배열은 다음 두 가지 방법 중 하나를 사용하여 메서드에 대한 인수를 지정할 수 있도록 허용합니다.
매개 변수 배열 형식으로 암시적으로 변환할 수 있는 형식의 단일 식입니다. 매개 변수 배열은 값 매개 변수로 작동합니다.
0개 이상의 인수로, 각 인수는 매개 변수 배열 요소의 형식으로 암시적으로 변환할 수 있는 형식의 식입니다.
다음 섹션의 예제에서는 두 호출 규칙을 모두 보여 줍니다.
메모
일반적으로 코드에서 ParamArrayAttribute 직접 사용되지 않습니다. 대신 Visual Basic ParamArray, C#의 params 같은 개별 언어 키워드는 ParamArrayAttribute 클래스의 래퍼로 사용됩니다. C#과 같은 일부 언어는 언어 키워드를 사용해야 하고 사용을 금지 ParamArrayAttribute할 수도 있습니다.
오버로드 확인 중에 매개 변수 배열을 지원하는 컴파일러가 존재하지 않지만 매개 변수 배열을 포함하는 오버로드보다 매개 변수가 하나 적은 메서드 오버로드를 발견하면 매개 변수 배열을 포함하는 오버로드로 메서드를 대체합니다. 예를 들어 클래스에 String.Split() 없는 인스턴스 메서드에 String 대한 호출은 메서드에 대한 호출 String.Split(Char[]) 로 확인됩니다. 또한 컴파일러는 필요한 형식의 빈 배열을 메서드에 전달합니다. 즉, 매개 변수 배열의 요소를 처리할 때 길이가 0인 배열을 처리하도록 메서드를 항상 준비해야 합니다. 이 예제에서는 그림을 제공합니다.
특성 사용에 대한 자세한 내용은 특성을 참조하세요.
생성자
| Name | Description |
|---|---|
| ParamArrayAttribute() |
기본 속성을 사용하여 클래스의 새 인스턴스를 ParamArrayAttribute 초기화합니다. |
속성
| Name | Description |
|---|---|
| TypeId |
파생 클래스에서 구현되는 경우 이 Attribute대한 고유 식별자를 가져옵니다. (다음에서 상속됨 Attribute) |
메서드
| Name | Description |
|---|---|
| Equals(Object) |
이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다. (다음에서 상속됨 Attribute) |
| GetHashCode() |
이 인스턴스의 해시 코드를 반환합니다. (다음에서 상속됨 Attribute) |
| GetType() |
현재 인스턴스의 Type 가져옵니다. (다음에서 상속됨 Object) |
| IsDefaultAttribute() |
파생 클래스에서 재정의되는 경우 이 인스턴스의 값이 파생 클래스의 기본값인지 여부를 나타냅니다. (다음에서 상속됨 Attribute) |
| Match(Object) |
파생 클래스에서 재정의되는 경우 이 인스턴스가 지정된 개체와 같은지 여부를 나타내는 값을 반환합니다. (다음에서 상속됨 Attribute) |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |
명시적 인터페이스 구현
| Name | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
이름 집합을 해당 디스패치 식별자 집합에 매핑합니다. (다음에서 상속됨 Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
인터페이스의 형식 정보를 가져오는 데 사용할 수 있는 개체의 형식 정보를 검색합니다. (다음에서 상속됨 Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
개체가 제공하는 형식 정보 인터페이스의 수를 검색합니다(0 또는 1). (다음에서 상속됨 Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
개체에 의해 노출되는 속성 및 메서드에 대한 액세스를 제공합니다. (다음에서 상속됨 Attribute) |
적용 대상
추가 정보
- Attribute
- 특성 사용하여 메타데이터 확장