ParamArrayAttribute クラス

定義

可変個数の引数を指定して呼び出せるメソッドであることを示します。 このクラスは継承できません。

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 1 つ以上の書式設定された温度値を Display 表示することを目的とした メソッドを含むクラスを定義します。 メソッドには、 formatsパラメーター配列として定義されている 1 つのパラメーター があります。

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

次の例は、 メソッドの 3 つの異なる呼び出しを Temperature.Display 示しています。 最初の では、 メソッドに書式指定文字列の配列が渡されます。 2 番目のメソッドには、4 つの個別の書式指定文字列が引数として渡されます。 3 つ目では、 メソッドは引数なしで呼び出されます。 この例の出力が示すように、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 、メソッド パラメーターがパラメーター配列であることを示します。 パラメーター配列を使用すると、不明な数の引数を指定できます。 パラメーター配列は仮パラメーター リストの最後のパラメーターである必要があり、単一次元配列である必要があります。 メソッドが呼び出されると、パラメーター配列では、次の 2 つの方法のいずれかでメソッドへの引数を指定できます。

  • パラメーター配列型に暗黙的に変換できる型の単一の式として。 パラメーター配列は、値パラメーターとして機能します。

  • 0 個以上の引数として。各引数は、パラメーター配列要素の型に暗黙的に変換できる型の式です。

次のセクションの例では、両方の呼び出し規則を示します。

注意

通常、 ParamArrayAttribute はコードで直接使用されません。 代わりに、Visual Basic や params C# などのParamArray個々の言語キーワードが、 クラスのParamArrayAttributeラッパーとして使用されます。 C# などの一部の言語では、language キーワードの使用が必要で、 の使用 ParamArrayAttributeが禁止されている場合もあります。

オーバーロードの解決中に、パラメーター配列をサポートするコンパイラが存在しないが、パラメーター配列を含むオーバーロードよりも 1 つ少ないパラメーターを持つメソッド オーバーロードを検出すると、メソッドはパラメーター配列を含むオーバーロードに置き換えられます。 たとえば、インスタンス メソッド (クラスにString存在しない) のString.Split()呼び出しは、 メソッドのString.Split(Char[])呼び出しとして解決されます。 コンパイラは、必要な型の空の配列も メソッドに渡します。 つまり、パラメーター配列内の要素を処理するときに、長さが 0 の配列を処理するには、常に メソッドを準備する必要があります。 具体的な例を次に示します。

属性の使用の詳細については、「 属性」を参照してください。

コンストラクター

ParamArrayAttribute()

ParamArrayAttribute クラスの新しいインスタンスを、既定のプロパティを使用して初期化します。

プロパティ

TypeId

派生クラスで実装されると、この Attribute の一意の識別子を取得します。

(継承元 Attribute)

メソッド

Equals(Object)

このインスタンスが、指定されたオブジェクトと等価であるかどうかを示す値を返します。

(継承元 Attribute)
GetHashCode()

このインスタンスのハッシュ コードを返します。

(継承元 Attribute)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IsDefaultAttribute()

派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

(継承元 Attribute)
Match(Object)

派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。

(継承元 Attribute)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

_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)

適用対象

こちらもご覧ください