Action<T1,T2,T3,T4> 대리자

정의

4개의 매개 변수가 있으며 반환하지 않는 메서드를 캡슐화합니다.

generic <typename T1, typename T2, typename T3, typename T4>
public delegate void Action(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate void Action<in T1,in T2,in T3,in T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
public delegate void Action<T1,T2,T3,T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
type Action<'T1, 'T2, 'T3, 'T4> = delegate of 'T1 * 'T2 * 'T3 * 'T4 -> unit
Public Delegate Sub Action(Of In T1, In T2, In T3, In T4)(arg1 As T1, arg2 As T2, arg3 As T3, arg4 As T4)
Public Delegate Sub Action(Of T1, T2, T3, T4)(arg1 As T1, arg2 As T2, arg3 As T3, arg4 As T4)

형식 매개 변수

T1

이 대리자로 캡슐화되는 메서드의 첫 번째 매개 변수 형식입니다.

이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.
T2

이 대리자로 캡슐화되는 메서드의 두 번째 매개 변수 형식입니다.

이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.
T3

이 대리자로 캡슐화되는 메서드의 세 번째 매개 변수 형식입니다.

이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.
T4

이 대리자로 캡슐화되는 메서드의 네 번째 매개 변수 형식입니다.

이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.

매개 변수

arg1
T1

이 대리자로 캡슐화되는 메서드의 첫 번째 매개 변수입니다.

arg2
T2

이 대리자로 캡슐화되는 메서드의 두 번째 매개 변수입니다.

arg3
T3

이 대리자로 캡슐화되는 메서드의 세 번째 매개 변수입니다.

arg4
T4

이 대리자로 캡슐화되는 메서드의 네 번째 매개 변수입니다.

설명

사용자 지정 대리자를 Action<T1,T2,T3,T4> 명시적으로 선언하지 않고 대리자를 사용하여 메서드를 매개 변수로 전달할 수 있습니다. 캡슐화 된 메서드에이 대리자에 의해 정의 되는 메서드 시그니처와 일치 해야 합니다. 즉, 캡슐화된 메서드에는 모두 값으로 전달되는 네 개의 매개 변수가 있어야 하며 값을 반환해서는 안 됩니다. (C#에서는 메서드 반환 해야 void합니다. F#에서 메서드 또는 함수는 단위를 반환해야 합니다. Visual Basic의 경우에서 정의 되어야 합니다는 Sub...End Sub 구문입니다. 또한 수 무시 되는 값을 반환 하는 메서드입니다.) 일반적으로 이러한 메서드는 작업을 수행 하는 합니다.

참고

매개 변수가 4개이고 값을 반환하는 메서드를 참조하려면 대신 제네릭 Func<T1,T2,T3,T4,TResult> 대리자를 사용합니다.

사용 하는 경우는 Action<T1,T2,T3,T4> 대리자 필요가 없습니다를 명시적으로 4 개의 매개 변수를 사용 하 여 메서드를 캡슐화 하는 대리자를 정의 합니다. 예를 들어 다음 코드는 명명 StringCopy 된 대리자를 명시적으로 선언하고 메서드에 CopyStrings 대한 참조를 해당 대리자 인스턴스에 할당합니다.

using System;

delegate void StringCopy(string[] stringArray1,
                         string[] stringArray2,
                         int indexToStart,
                         int numberToCopy);

public class TestDelegate
{
   public static void Main()
   {
      string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
                           "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
      string[] copiedOrdinals = new string[ordinals.Length];
      StringCopy copyOperation = CopyStrings;
      copyOperation(ordinals, copiedOrdinals, 3, 5);
      foreach (string ordinal in copiedOrdinals)
         Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
   }

   private static void CopyStrings(string[] source, string[] target,
                                   int startPos, int number)
   {
      if (source.Length != target.Length)
         throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");

      for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
         target[ctr] = string.Copy(source[ctr]);
   }
}
open System

type StringCopy = delegate of stringArray1: string [] *
                              stringArray2: string [] *
                              indexToStart: int *
                              numberToCopy: int -> unit

let copyStrings (source: string []) (target: string []) startPos number =
    if source.Length <> target.Length then
        raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.")

    for i = startPos to startPos + number - 1 do
        target.[i] <- source.[i]

let ordinals =
    [| "First"; "Second"; "Third"; "Fourth"; "Fifth"
       "Sixth"; "Seventh"; "Eighth"; "Ninth"; "Tenth" |]

let copiedOrdinals: string [] = Array.zeroCreate ordinals.Length

let copyOperation = StringCopy copyStrings

copyOperation.Invoke(ordinals, copiedOrdinals, 3, 5)

for ordinal in copiedOrdinals do
    printfn "%s" (if String.IsNullOrEmpty ordinal then "<None>" else ordinal)
Delegate Sub StringCopy(stringArray1() As String, _
                        stringArray2() As String, _
                        indexToStart As Integer, _
                        numberToCopy As Integer)

Module TestDelegate
   Public Sub Main()
      Dim ordinals() As String = {"First", "Second", "Third", "Fourth", _
                                  "Fifth", "Sixth", "Seventh", "Eighth", _
                                  "Ninth", "Tenth"}
      Dim copiedOrdinals(ordinals.Length - 1) As String
      Dim copyOperation As StringCopy = AddressOf CopyStrings
      copyOperation(ordinals, copiedOrdinals, 3, 5)
      For Each ordinal As String In copiedOrdinals
         Console.WriteLine(ordinal)
      Next    
   End Sub
   
   Private Sub CopyStrings(source() As String, target() As String, _
                           startPos As Integer, number As Integer)
      If source.Length <> target.Length Then 
         Throw New IndexOutOfRangeException("The source and target arrays" & _
                   " must have the same number of elements.")
      End If
      For ctr As Integer = startPos to startpos + number  - 1
         target(ctr) = String.Copy(source(ctr))
      Next
   End Sub
End Module

다음 예제에서는 인스턴스화하여이 코드를 간소화는 Action<T1,T2,T3,T4> 명시적으로 새 대리자를 정의 하 고 명명된 된 메서드를 할당 하는 대신 대리자입니다.

using System;

public class TestAction4
{
   public static void Main()
   {
      string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
                           "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
      string[] copiedOrdinals = new string[ordinals.Length];
      Action<string[], string[], int, int> copyOperation = CopyStrings;
      copyOperation(ordinals, copiedOrdinals, 3, 5);
      foreach (string ordinal in copiedOrdinals)
         Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
   }

   private static void CopyStrings(string[] source, string[] target,
                                   int startPos, int number)
   {
      if (source.Length != target.Length)
         throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");

      for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
         target[ctr] = string.Copy(source[ctr]);
   }
}
open System

let copyStrings (source: string []) (target: string []) startPos number =
    if source.Length <> target.Length then
        raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.")

    for i = startPos to startPos + number - 1 do
        target.[i] <- source.[i]

let ordinals =
    [| "First"; "Second"; "Third"; "Fourth"; "Fifth"
       "Sixth"; "Seventh"; "Eighth"; "Ninth"; "Tenth" |]

let copiedOrdinals: string [] = Array.zeroCreate ordinals.Length

let copyOperation = Action<_, _, _, _> copyStrings

copyOperation.Invoke(ordinals, copiedOrdinals, 3, 5)

for ordinal in copiedOrdinals do
    printfn "%s" (if String.IsNullOrEmpty ordinal then "<None>" else ordinal)
Module TestAction4
   Public Sub Main()
      Dim ordinals() As String = {"First", "Second", "Third", "Fourth", _
                                  "Fifth", "Sixth", "Seventh", "Eighth", _
                                  "Ninth", "Tenth"}
      Dim copiedOrdinals(ordinals.Length - 1) As String
      Dim copyOperation As Action(Of String(), String(), Integer, Integer) = _
                           AddressOf CopyStrings
      copyOperation(ordinals, copiedOrdinals, 3, 5)
      For Each ordinal As String In copiedOrdinals
         Console.WriteLine(ordinal)
      Next    
   End Sub
   
   Private Sub CopyStrings(source() As String, target() As String, _
                           startPos As Integer, number As Integer)
      If source.Length <> target.Length Then 
         Throw New IndexOutOfRangeException("The source and target arrays" & _
                   " must have the same number of elements.")
      End If
      For ctr As Integer = startPos to startpos + number  - 1
         target(ctr) = String.Copy(source(ctr))
      Next
   End Sub
End Module

사용할 수도 있습니다는 Action<T1,T2,T3,T4> 다음 예제와 같이 C#에서는 무명 메서드로 위임 합니다. (소개 무명 메서드를 참조 하세요 무명 메서드.)

using System;

public class TestAnonymousMethod
{
   public static void Main()
   {
      string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
                           "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
      string[] copiedOrdinals = new string[ordinals.Length];
      Action<string[], string[], int, int> copyOperation =
                                           delegate(string[] s1, string[] s2,
                                           int pos, int num)
                                { CopyStrings(s1, s2, pos, num); };
      copyOperation(ordinals, copiedOrdinals, 3, 5);
      foreach (string ordinal in copiedOrdinals)
         Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
   }

   private static void CopyStrings(string[] source, string[] target,
                                   int startPos, int number)
   {
      if (source.Length != target.Length)
         throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");

      for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
         target[ctr] = string.Copy(source[ctr]);
   }
}

람다 식을 할당할 수도 있습니다는 Action<T1,T2,T3,T4> 다음 예제와 같이 인스턴스를 위임 합니다. (람다 식에 대한 소개는 람다 식(C#) 또는 람다 식(F#)을 참조하세요.)

using System;

public class TestLambdaExpression
{
   public static void Main()
   {
      string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
                           "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
      string[] copiedOrdinals = new string[ordinals.Length];
      Action<string[], string[], int, int> copyOperation = (s1, s2, pos, num)
                                           => CopyStrings(s1, s2, pos, num);
      copyOperation(ordinals, copiedOrdinals, 3, 5);
      foreach (string ordinal in copiedOrdinals)
         Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
   }

   private static void CopyStrings(string[] source, string[] target,
                                   int startPos, int number)
   {
      if (source.Length != target.Length)
         throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");

      for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
         target[ctr] = string.Copy(source[ctr]);
   }
}
open System

let copyStrings (source: string []) (target: string []) startPos number =
    if source.Length <> target.Length then
        raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.")

    for i = startPos to startPos + number - 1 do
        target.[i] <- source.[i]

let ordinals =
    [| "First"; "Second"; "Third"; "Fourth"; "Fifth"
       "Sixth"; "Seventh"; "Eighth"; "Ninth"; "Tenth" |]

let copiedOrdinals: string [] = Array.zeroCreate ordinals.Length

let copyOperation = Action<_, _, _, _> (fun s1 s2 pos num -> copyStrings s1 s2 pos num)

copyOperation.Invoke(ordinals, copiedOrdinals, 3, 5)

for ordinal in copiedOrdinals do
    printfn "%s" (if String.IsNullOrEmpty ordinal then "<None>" else ordinal)
Public Module TestLambdaExpression
   Public Sub Main()
      Dim ordinals() As String = {"First", "Second", "Third", "Fourth", "Fifth", _ 
                           "Sixth", "Seventh", "Eighth", "Ninth", "Tenth"}
      Dim copiedOrdinals(ordinals.Length - 1) As String           
      Dim copyOperation As Action(Of String(), String(), Integer, Integer) = _
                           Sub(s1, s2, pos, num) CopyStrings(s1, s2, pos, num)  
      copyOperation(ordinals, copiedOrdinals, 3, 5)
      For Each ordinal As String In copiedOrdinals
         If String.IsNullOrEmpty(ordinal) Then
            Console.WriteLine("<None>")
         Else
            Console.WriteLine(ordinal)
         End If
      Next   
   End Sub

   Private Function CopyStrings(source() As String, target() As String, _ 
                                startPos As Integer, number As Integer) As Integer
      If source.Length <> target.Length Then 
         throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.")
      End If
      
      For ctr As Integer = startPos To startPos + number - 1
         target(ctr) = String.Copy(source(ctr))
      Next
      Return number
   End Function
End Module
' The example displays the following output:
'       Fourth
'       Fifth
'       Sixth
'       Seventh
'       Eighth

확장 메서드

GetMethodInfo(Delegate)

지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다.

적용 대상

추가 정보