Action<T1,T2> 대리자
정의
2개의 매개 변수가 있으며 값을 반환하지 않는 메서드를 캡슐화합니다.Encapsulates a method that has two parameters and does not return a value.
generic <typename T1, typename T2>
public delegate void Action(T1 arg1, T2 arg2);
public delegate void Action<in T1,in T2>(T1 arg1, T2 arg2);
public delegate void Action<T1,T2>(T1 arg1, T2 arg2);
type Action<'T1, 'T2> = delegate of 'T1 * 'T2 -> unit
Public Delegate Sub Action(Of In T1, In T2)(arg1 As T1, arg2 As T2)
Public Delegate Sub Action(Of T1, T2)(arg1 As T1, arg2 As T2)
형식 매개 변수
- T1
이 대리자로 캡슐화되는 메서드의 첫 번째 매개 변수 형식입니다.The type of the first parameter of the method that this delegate encapsulates.
이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.- T2
이 대리자로 캡슐화되는 메서드의 두 번째 매개 변수 형식입니다.The type of the second parameter of the method that this delegate encapsulates.
이 형식 매개 변수는 반공변(Contravariant)입니다. 즉, 지정한 형식이나 더 적게 파생된 모든 형식을 사용할 수 있습니다. 공변성(Covariance) 및 반공변성(Contravariance)에 대한 자세한 내용은 제네릭의 공변성(Covariance) 및 반공변성(Contravariance)을 참조하세요.매개 변수
- arg1
- T1
이 대리자로 캡슐화되는 메서드의 첫 번째 매개 변수입니다.The first parameter of the method that this delegate encapsulates.
- arg2
- T2
이 대리자로 캡슐화되는 메서드의 두 번째 매개 변수입니다.The second parameter of the method that this delegate encapsulates.
설명
대리자를 사용 하 여 Action<T1,T2> 사용자 지정 대리자를 명시적으로 선언 하지 않고 메서드를 매개 변수로 전달할 수 있습니다.You can use the Action<T1,T2> delegate to pass a method as a parameter without explicitly declaring a custom delegate. 캡슐화 된 메서드에이 대리자에 의해 정의 되는 메서드 시그니처와 일치 해야 합니다.The encapsulated method must correspond to the method signature that is defined by this delegate. 즉, 캡슐화 된 메서드에는 모두 값으로 전달 되는 두 개의 매개 변수가 있어야 하 고 값을 반환 하지 않아야 합니다.This means that the encapsulated method must have two parameters that are both passed to it by value, and it must not return a value. (C#에서는 메서드 반환 해야 void
합니다.(In C#, the method must return void
. Visual Basic의 경우에서 정의 되어야 합니다는 Sub
...End Sub
In Visual Basic, it must be defined by the Sub
…End Sub
구문입니다.construct. 또한 수 무시 되는 값을 반환 하는 메서드입니다.) 일반적으로 이러한 메서드는 작업을 수행 하는 합니다.It can also be a method that returns a value that is ignored.) Typically, such a method is used to perform an operation.
참고
두 개의 매개 변수가 있으며 값을 반환 하는 메서드를 참조 하려면 제네릭 Func<T1,T2,TResult> 대리자를 대신 사용 합니다.To reference a method that has two parameters and returns a value, use the generic Func<T1,T2,TResult> delegate instead.
대리자를 사용 하는 경우 Action<T1,T2> 두 개의 매개 변수를 사용 하 여 메서드를 캡슐화 하는 대리자를 명시적으로 정의할 필요가 없습니다.When you use the Action<T1,T2> delegate, you do not have to explicitly define a delegate that encapsulates a method with two parameters. 예를 들어 다음 코드는 라는 대리자를 명시적으로 선언 합니다 ConcatStrings
.For example, the following code explicitly declares a delegate named ConcatStrings
. 그런 다음 두 메서드 중 하나에 대 한 참조를 해당 대리자 인스턴스에 할당 합니다.It then assigns a reference to either of two methods to its delegate instance. 한 메서드는 두 개의 문자열을 콘솔에 씁니다. 두 번째는 두 개의 문자열을 파일에 씁니다.One method writes two strings to the console; the second writes two strings to a file.
using System;
using System.IO;
delegate void ConcatStrings(string string1, string string2);
public class TestDelegate
{
public static void Main()
{
string message1 = "The first line of a message.";
string message2 = "The second line of a message.";
ConcatStrings concat;
if (Environment.GetCommandLineArgs().Length > 1)
concat = WriteToFile;
else
concat = WriteToConsole;
concat(message1, message2);
}
private static void WriteToConsole(string string1, string string2)
{
Console.WriteLine("{0}\n{1}", string1, string2);
}
private static void WriteToFile(string string1, string string2)
{
StreamWriter writer = null;
try
{
writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
writer.WriteLine("{0}\n{1}", string1, string2);
}
catch
{
Console.WriteLine("File write operation failed...");
}
finally
{
if (writer != null) writer.Close();
}
}
}
Imports System.IO
Delegate Sub ConcatStrings(string1 As String, string2 As String)
Module TestDelegate
Public Sub Main()
Dim message1 As String = "The first line of a message."
Dim message2 As String = "The second line of a message."
Dim concat As ConcatStrings
If Environment.GetCommandLineArgs().Length > 1 Then
concat = AddressOf WriteToFile
Else
concat = AddressOf WriteToConsole
End If
concat(message1, message2)
End Sub
Private Sub WriteToConsole(string1 As String, string2 As String)
Console.WriteLine("{0}{1}{2}", string1, vbCrLf, string2)
End Sub
Private Sub WriteToFile(string1 As String, string2 As String)
Dim writer As StreamWriter = Nothing
Try
writer = New StreamWriter(Environment.GetCommandLineArgs(1), False)
writer.WriteLine("{0}{1}{2}", string1, vbCrLf, string2)
Catch
Console.WriteLine("File write operation failed...")
Finally
If writer IsNot Nothing Then writer.Close
End Try
End Sub
End Module
다음 예제에서는 인스턴스화하여이 코드를 간소화는 Action<T1,T2> 명시적으로 새 대리자를 정의 하 고 명명된 된 메서드를 할당 하는 대신 대리자입니다.The following example simplifies this code by instantiating the Action<T1,T2> delegate instead of explicitly defining a new delegate and assigning a named method to it.
using System;
using System.IO;
public class TestAction2
{
public static void Main()
{
string message1 = "The first line of a message.";
string message2 = "The second line of a message.";
Action<string, string> concat;
if (Environment.GetCommandLineArgs().Length > 1)
concat = WriteToFile;
else
concat = WriteToConsole;
concat(message1, message2);
}
private static void WriteToConsole(string string1, string string2)
{
Console.WriteLine("{0}\n{1}", string1, string2);
}
private static void WriteToFile(string string1, string string2)
{
StreamWriter writer = null;
try
{
writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
writer.WriteLine("{0}\n{1}", string1, string2);
}
catch
{
Console.WriteLine("File write operation failed...");
}
finally
{
if (writer != null) writer.Close();
}
}
}
Imports System.IO
Module TestAction2
Public Sub Main()
Dim message1 As String = "The first line of a message."
Dim message2 As String = "The second line of a message."
Dim concat As Action(Of String, String)
If Environment.GetCommandLineArgs().Length > 1 Then
concat = AddressOf WriteToFile
Else
concat = AddressOf WriteToConsole
End If
concat(message1, message2)
End Sub
Private Sub WriteToConsole(string1 As String, string2 As String)
Console.WriteLine("{0}{1}{2}", string1, vbCrLf, string2)
End Sub
Private Sub WriteToFile(string1 As String, string2 As String)
Dim writer As StreamWriter = Nothing
Try
writer = New StreamWriter(Environment.GetCommandLineArgs(1), False)
writer.WriteLine("{0}{1}{2}", string1, vbCrLf, string2)
Catch
Console.WriteLine("File write operation failed...")
Finally
If writer IsNot Nothing Then writer.Close
End Try
End Sub
End Module
사용할 수도 있습니다는 Action<T1,T2> 다음 예제와 같이 C#에서는 무명 메서드로 위임 합니다.You can also use the Action<T1,T2> delegate with anonymous methods in C#, as the following example illustrates. (소개 무명 메서드를 참조 하세요 무명 메서드.)(For an introduction to anonymous methods, see Anonymous Methods.)
using System;
using System.IO;
public class TestAnonymousMethod
{
public static void Main()
{
string message1 = "The first line of a message.";
string message2 = "The second line of a message.";
Action<string, string> concat;
if (Environment.GetCommandLineArgs().Length > 1)
concat = delegate(string s1, string s2) { WriteToFile(s1, s2); };
else
concat = delegate(string s1, string s2) { WriteToConsole(s1, s2);} ;
concat(message1, message2);
}
private static void WriteToConsole(string string1, string string2)
{
Console.WriteLine("{0}\n{1}", string1, string2);
}
private static void WriteToFile(string string1, string string2)
{
StreamWriter writer = null;
try
{
writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
writer.WriteLine("{0}\n{1}", string1, string2);
}
catch
{
Console.WriteLine("File write operation failed...");
}
finally
{
if (writer != null) writer.Close();
}
}
}
람다 식을 할당할 수도 있습니다는 Action<T1,T2> 다음 예제와 같이 인스턴스를 위임 합니다.You can also assign a lambda expression to an Action<T1,T2> delegate instance, as the following example illustrates. (람다 식에 대 한 소개를 참조 하세요 람다 식.)(For an introduction to lambda expressions, see Lambda Expressions.)
using System;
using System.IO;
public class TestLambdaExpression
{
public static void Main()
{
string message1 = "The first line of a message.";
string message2 = "The second line of a message.";
Action<string, string> concat;
if (Environment.GetCommandLineArgs().Length > 1)
concat = (s1, s2) => WriteToFile(s1, s2);
else
concat = (s1, s2) => WriteToConsole(s1, s2);
concat(message1, message2);
}
private static void WriteToConsole(string string1, string string2)
{
Console.WriteLine("{0}\n{1}", string1, string2);
}
private static void WriteToFile(string string1, string string2)
{
StreamWriter writer = null;
try
{
writer = new StreamWriter(Environment.GetCommandLineArgs()[1], false);
writer.WriteLine("{0}\n{1}", string1, string2);
}
catch
{
Console.WriteLine("File write operation failed...");
}
finally
{
if (writer != null) writer.Close();
}
}
}
Imports System.IO
Public Module TestLambdaExpression
Public Sub Main()
Dim message1 As String = "The first line of a message."
Dim message2 As String = "The second line of a message."
Dim concat As Action(Of String, String)
If Environment.GetCommandLineArgs().Length > 1 Then
concat = Sub(s1, s2) WriteToFile(s1, s2)
Else
concat = Sub(s1, s2) WriteToConsole(s1, s2)
End If
concat(message1, message2)
End Sub
Private Function WriteToConsole(string1 As String, string2 As String) As Integer
Dim message As String = String.Format("{0}{1}{2}", string1, vbCrLf, string2)
Console.WriteLine(message)
Return message.Length
End Function
Private Function WriteToFile(string1 As String, string2 As String) As Integer
Dim writer As StreamWriter = Nothing
Dim message As String = String.Format("{0}{1}{2}", string1, vbCrLf, string2)
Dim charsWritten As Integer
Try
writer = New StreamWriter(Environment.GetCommandLineArgs()(1), False)
writer.WriteLine(message)
Catch
Console.WriteLine("File write operation failed...")
Finally
If writer IsNot Nothing Then
writer.Close()
charsWritten = message.Length
Else
charsWritten = 0
End If
End Try
Return charsWritten
End Function
End Module
확장 메서드
GetMethodInfo(Delegate) |
지정된 대리자가 나타내는 메서드를 나타내는 개체를 가져옵니다.Gets an object that represents the method represented by the specified delegate. |