using System;
using static System.Console;
//定义委托类型时可以指定输出参数、引用参数、可选参数、参数数组
delegate void Del(out int a,ref int b,int c=77777,params int[] arr);
class Exam
{
static void Main(string[] args)
{
int a=1,b=2;
f(out a,ref b,555,6,7,8);
f(out a,ref b);
g(out a,ref b,999,6,7,8);
g(out a,ref b);
WriteLine("############");
Del d=f;
d+=g;
//可选参数c使用的也是定义委托Del时的默认值77777,未使用函数f和g的默认值,
//参数数组为空
d(out a,ref b);
b=112233;
//匿名方法的参数不允许可选参数和参数数组
d+=delegate(out int a2,ref int b2,int c2/*=444*/,/*params*/ int[] arr2){
a2=123;
WriteLine($"in delegate {a2},{b2},{c2},{String.Join("+",arr2)}");
};
//lambda表达式不允许有可选参数和参数数组
d+=(out int a3,ref int b3,int c3/*=444*/,/*params*/ int[] arr3)=>{
a3=456;
WriteLine($"in lambda表达式 {a3},{b3},{c3},{String.Join("+",arr3)}");
};
d(out a,ref b,445566,8,8,8);
b=666888;
//可选参数c使用定义委托Del时的默认值77777,参数数组为空
d(out a,ref b);
ReadKey();
}
static void f(out int a,ref int b,int c=88,params int[] arr)
{
//输出参数使用前需赋值
a=111;
WriteLine($"{a},{b}");
Action<int> d=delegate(int x)
{
WriteLine($"{c},{x}");
//匿名方法内部不能使用输出参数、引用参数
//WriteLine($"{a},{b}");
WriteLine($"{String.Join("+",arr)}");
};
Action<int> d2=(int x)=>{
WriteLine($"{c},{x}");
//lambda表达式内部不能使用输出参数、引用参数
//WriteLine($"{a},{b}");
WriteLine($"{String.Join("+",arr)}");
};
d(4);
d2(44);
}
//输出参数、引用参数、参数数组不能指定默认值
//static void g(out int a=1,ref int b=2,params int[] arr=new int[]{5,6,7})
static void g(out int a,ref int b,int c=999,params int[] arr)
{
a=222;
WriteLine($"{a},{b},{c},{String.Join("+",arr)}");
}
}


using static System.Console;
//定义委托类型时可以指定输出参数、引用参数、可选参数、参数数组
delegate void Del(out int a,ref int b,int c=77777,params int[] arr);
class Exam
{
static void Main(string[] args)
{
int a=1,b=2;
f(out a,ref b,555,6,7,8);
f(out a,ref b);
g(out a,ref b,999,6,7,8);
g(out a,ref b);
WriteLine("############");
Del d=f;
d+=g;
//可选参数c使用的也是定义委托Del时的默认值77777,未使用函数f和g的默认值,
//参数数组为空
d(out a,ref b);
b=112233;
//匿名方法的参数不允许可选参数和参数数组
d+=delegate(out int a2,ref int b2,int c2/*=444*/,/*params*/ int[] arr2){
a2=123;
WriteLine($"in delegate {a2},{b2},{c2},{String.Join("+",arr2)}");
};
//lambda表达式不允许有可选参数和参数数组
d+=(out int a3,ref int b3,int c3/*=444*/,/*params*/ int[] arr3)=>{
a3=456;
WriteLine($"in lambda表达式 {a3},{b3},{c3},{String.Join("+",arr3)}");
};
d(out a,ref b,445566,8,8,8);
b=666888;
//可选参数c使用定义委托Del时的默认值77777,参数数组为空
d(out a,ref b);
ReadKey();
}
static void f(out int a,ref int b,int c=88,params int[] arr)
{
//输出参数使用前需赋值
a=111;
WriteLine($"{a},{b}");
Action<int> d=delegate(int x)
{
WriteLine($"{c},{x}");
//匿名方法内部不能使用输出参数、引用参数
//WriteLine($"{a},{b}");
WriteLine($"{String.Join("+",arr)}");
};
Action<int> d2=(int x)=>{
WriteLine($"{c},{x}");
//lambda表达式内部不能使用输出参数、引用参数
//WriteLine($"{a},{b}");
WriteLine($"{String.Join("+",arr)}");
};
d(4);
d2(44);
}
//输出参数、引用参数、参数数组不能指定默认值
//static void g(out int a=1,ref int b=2,params int[] arr=new int[]{5,6,7})
static void g(out int a,ref int b,int c=999,params int[] arr)
{
a=222;
WriteLine($"{a},{b},{c},{String.Join("+",arr)}");
}
}

