Action<T1,T2,T3> 대리자
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
매개 변수가 3개이고 값을 반환하지 않는 메서드를 캡슐화합니다.
generic <typename T1, typename T2, typename T3>
public delegate void Action(T1 arg1, T2 arg2, T3 arg3);
public delegate void Action<in T1,in T2,in T3>(T1 arg1, T2 arg2, T3 arg3);
public delegate void Action<T1,T2,T3>(T1 arg1, T2 arg2, T3 arg3);
type Action<'T1, 'T2, 'T3> = delegate of 'T1 * 'T2 * 'T3 -> unit
Public Delegate Sub Action(Of In T1, In T2, In T3)(arg1 As T1, arg2 As T2, arg3 As T3)
Public Delegate Sub Action(Of T1, T2, T3)(arg1 As T1, arg2 As T2, arg3 As T3)
형식 매개 변수
- T1
이 대리자로 캡슐화되는 메서드의 첫 번째 매개 변수 형식입니다.
이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.- T2
이 대리자로 캡슐화되는 메서드의 두 번째 매개 변수 형식입니다.
이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.- T3
이 대리자로 캡슐화되는 메서드의 세 번째 매개 변수 형식입니다.
이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.매개 변수
- arg1
- T1
이 대리자로 캡슐화되는 메서드의 첫 번째 매개 변수입니다.
- arg2
- T2
이 대리자로 캡슐화되는 메서드의 두 번째 매개 변수입니다.
- arg3
- T3
이 대리자로 캡슐화되는 메서드의 세 번째 매개 변수입니다.
설명
사용자 지정 대리자를 Action<T1,T2,T3> 명시적으로 선언하지 않고 대리자를 사용하여 메서드를 매개 변수로 전달할 수 있습니다. 캡슐화 된 메서드에이 대리자에 의해 정의 되는 메서드 시그니처와 일치 해야 합니다. 즉, 캡슐화된 메서드에는 값으로 전달되는 세 개의 매개 변수가 있어야 하며 값을 반환해서는 안 됩니다. (C#에서 메서드는 를 반환void
해야 합니다. F#에서 메서드 또는 함수는 단위를 반환해야 합니다. Visual Basic에서는 ...End Sub
구문으로 Sub
정의해야 합니다. 무시되는 값을 반환하는 메서드일 수도 있습니다.) 일반적으로 이러한 메서드는 작업을 수행하는 데 사용됩니다.
참고
세 개의 매개 변수가 있고 값을 반환하는 메서드를 참조하려면 대신 제네릭 Func<T1,T2,T3,TResult> 대리자를 사용합니다.
대리자를 Action<T1,T2,T3> 사용하는 경우 메서드를 세 개의 매개 변수로 캡슐화하는 대리자를 명시적으로 정의할 필요가 없습니다. 예를 들어 다음 코드는 라는 StringCopy
대리자를 명시적으로 선언하고 해당 대리자 인스턴스에 메서드에 CopyStrings
대한 참조를 할당합니다.
using System;
delegate void StringCopy(string[] stringArray1,
string[] stringArray2,
int indexToStart);
public class TestDelegate
{
public static void Main()
{
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth"};
string[] copiedOrdinals = new string[ordinals.Length];
StringCopy copyOperation = CopyStrings;
copyOperation(ordinals, copiedOrdinals, 3);
foreach (string ordinal in copiedOrdinals)
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
}
private static void CopyStrings(string[] source, string[] target, int startPos)
{
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 <= source.Length - 1; ctr++)
target[ctr] = string.Copy(source[ctr]);
}
}
open System
type StringCopy = delegate of stringArray1: string [] *
stringArray2: string [] *
indexToStart: int -> unit
let copyStrings (source: string []) (target: string []) startPos =
if source.Length <> target.Length then
raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.")
for i = startPos to source.Length - 1 do
target.[i] <- source.[i]
let ordinals = [| "First"; "Second"; "Third"; "Fourth"; "Fifth" |]
let copiedOrdinals: string [] = Array.zeroCreate ordinals.Length
let copyOperation = StringCopy copyStrings
copyOperation.Invoke(ordinals, copiedOrdinals, 3)
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)
Module TestDelegate
Public Sub Main()
Dim ordinals() As String = {"First", "Second", "Third", "Fourth", "Fifth"}
Dim copiedOrdinals(ordinals.Length - 1) As String
Dim copyOperation As StringCopy = AddressOf CopyStrings
copyOperation(ordinals, copiedOrdinals, 3)
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)
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 source.Length - 1
target(ctr) = String.Copy(source(ctr))
Next
End Sub
End Module
다음 예제에서는 인스턴스화하여이 코드를 간소화는 Action<T1,T2,T3> 명시적으로 새 대리자를 정의 하 고 명명된 된 메서드를 할당 하는 대신 대리자입니다.
using System;
public class TestAction3
{
public static void Main()
{
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth"};
string[] copiedOrdinals = new string[ordinals.Length];
Action<string[], string[], int> copyOperation = CopyStrings;
copyOperation(ordinals, copiedOrdinals, 3);
foreach (string ordinal in copiedOrdinals)
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
}
private static void CopyStrings(string[] source, string[] target, int startPos)
{
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 <= source.Length - 1; ctr++)
target[ctr] = string.Copy(source[ctr]);
}
}
open System
let copyStrings (source: string []) (target: string []) startPos =
if source.Length <> target.Length then
raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.")
for i = startPos to source.Length - 1 do
target.[i] <- source.[i]
let ordinals = [| "First"; "Second"; "Third"; "Fourth"; "Fifth" |]
let copiedOrdinals = Array.zeroCreate<string> ordinals.Length
let copyOperation = Action<_,_,_> copyStrings
copyOperation.Invoke(ordinals, copiedOrdinals, 3)
for ordinal in copiedOrdinals do
printfn "%s" (if String.IsNullOrEmpty ordinal then "<None>" else ordinal)
Module TestAction3
Public Sub Main()
Dim ordinals() As String = {"First", "Second", "Third", "Fourth", "Fifth"}
Dim copiedOrdinals(ordinals.Length - 1) As String
Dim copyOperation As Action(Of String(), String(), Integer) = AddressOf CopyStrings
copyOperation(ordinals, copiedOrdinals, 3)
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)
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 source.Length - 1
target(ctr) = String.Copy(source(ctr))
Next
End Sub
End Module
사용할 수도 있습니다는 Action<T1,T2,T3> 다음 예제와 같이 C#에서는 무명 메서드로 위임 합니다. (소개 무명 메서드를 참조 하세요 무명 메서드.)
using System;
public class TestAnon
{
public static void Main()
{
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth"};
string[] copiedOrdinals = new string[ordinals.Length];
Action<string[], string[], int> copyOperation = delegate(string[] s1,
string[] s2,
int pos)
{ CopyStrings(s1, s2, pos); };
copyOperation(ordinals, copiedOrdinals, 3);
foreach (string ordinal in copiedOrdinals)
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
}
private static void CopyStrings(string[] source, string[] target, int startPos)
{
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 <= source.Length - 1; ctr++)
target[ctr] = string.Copy(source[ctr]);
}
}
람다 식을 할당할 수도 있습니다는 Action<T1,T2,T3> 다음 예제와 같이 인스턴스를 위임 합니다. 람다 식에 대한 소개는 람다 식(C#) 또는 람다 식(F#)을 참조하세요.
using System;
public class TestLambda
{
public static void Main()
{
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth"};
string[] copiedOrdinals = new string[ordinals.Length];
Action<string[], string[], int> copyOperation = (s1, s2, pos) =>
CopyStrings(s1, s2, pos);
copyOperation(ordinals, copiedOrdinals, 3);
foreach (string ordinal in copiedOrdinals)
Console.WriteLine(ordinal == string.Empty ? "<None>" : ordinal);
}
private static void CopyStrings(string[] source, string[] target, int startPos)
{
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 <= source.Length - 1; ctr++)
target[ctr] = string.Copy(source[ctr]);
}
}
open System
let copyStrings (source: string []) (target: string []) startPos =
if source.Length <> target.Length then
raise (IndexOutOfRangeException "The source and target arrays must have the same number of elements.")
for i = startPos to source.Length - 1 do
target.[i] <- source.[i]
let ordinals = [| "First"; "Second"; "Third"; "Fourth"; "Fifth" |]
let copiedOrdinals: string [] = Array.zeroCreate ordinals.Length
let copyOperation = Action<_,_,_> (fun s1 s2 pos -> copyStrings s1 s2 pos)
copyOperation.Invoke(ordinals, copiedOrdinals, 3)
for ordinal in copiedOrdinals do
printfn "%s" (if String.IsNullOrEmpty ordinal then "<None>" else ordinal)
Public Module TestLambda
Public Sub Main()
Dim ordinals() As String = {"First", "Second", "Third", "Fourth", "Fifth"}
Dim copiedOrdinals(ordinals.Length - 1) As String
Dim copyOperation As Action(Of String(), String(), Integer) = _
Sub(s1, s2, pos) CopyStrings(s1, s2, pos)
copyOperation(ordinals, copiedOrdinals, 3)
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) 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 source.Length - 1
target(ctr) = String.Copy(source(ctr))
Next
Return source.Length - startPos
End Function
End Module
' The example displays the following output:
' Fourth
' Fifth
확장 메서드
GetMethodInfo(Delegate) |
지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다. |
적용 대상
추가 정보
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기