728x90
public partial class Program
{
static void Main(string[] args)
{
// 하나의 제네릭 매개 변수 사용
GenericDelegateInvoke();
// 여러 개의 제네릭 매개 변수를 사용
VoidDelegateInvoke();
// 반환값을 갖는 다중 템플릿 대리자
ReturnValueDelegateInvoke();
}
}
public partial class Program
{
private delegate T FormulaDelegate<T>(T a, T b);
private static int AddInt(int x, int y)
{
return x + y;
}
private static double AddDouble(double x, double y)
{
return x + y;
}
private static void GenericDelegateInvoke()
{
FormulaDelegate<int> intAddition = AddInt;
FormulaDelegate<double> DoubleAddition = AddDouble;
Console.WriteLine("Invoking intAddition(2, 3)");
Console.WriteLine("Result = {0}", intAddition(2, 3));
Console.WriteLine("Invoking doubleAddition(2.2 , 3.5)");
Console.WriteLine("Result = {0}", DoubleAddition(2.2, 3.5));
}
}
public partial class Program
{
private delegate void AdditionDelegate<T1, T2>(T1 value1, T2 value2);
private static void AddIntDouble(int x, double y)
{
Console.WriteLine($"int {x} + double {y} = {x + y}");
}
private static void AddFloatDouble(float x, double y)
{
Console.WriteLine($"float {x} + double {y} = {x + y}");
}
private static void VoidDelegateInvoke()
{
AdditionDelegate<int, double> intDoubleAdd = AddIntDouble;
AdditionDelegate<float, double> floatDoubleAdd = AddFloatDouble;
intDoubleAdd(1, 6.5);
floatDoubleAdd((float)5.5, 4.3);
}
}
public partial class Program
{
private delegate TResult AddAndConvert<T1, T2, TResult>(T1 digit1, T2 digit2);
private static float AddIntDoubleConvert(int x, double y)
{
float result = (float)(x + y);
Console.WriteLine($"(int) {x} + (double) {y} = (float) {result}");
return result;
}
private static int AddFloatDoubleConvert(float x, double y)
{
int result = (int)(x + y);
Console.WriteLine($"(float) {x} + (double) {y} = (int) {result}");
return result;
}
private static void ReturnValueDelegateInvoke()
{
AddAndConvert<int, double, float> intDoubleAddConvertToFloat = AddIntDoubleConvert;
AddAndConvert<float, double, int> floatDoubleAddConvertToInt = AddFloatDoubleConvert;
float f = intDoubleAddConvertToFloat(5, 3.8);
int i = floatDoubleAddConvertToInt((float)6.4, 9.8);
}
}
728x90
'c#' 카테고리의 다른 글
System.InvalidCastException(System.Data.DataSetExtensions.dll) (0) | 2023.01.16 |
---|---|
[C#] Delegate Action_Func (0) | 2023.01.15 |
[C#] Delegate 멀티캐스트 (0) | 2023.01.14 |
[C#] Delegate3 (0) | 2023.01.14 |
[C#] Delegate2 (0) | 2023.01.14 |