ufcpp/UfcppSample

Lambda default parameters

Closed this issue · 3 comments

ufcpp commented

リフレクションで情報取ったとき、Delegate.Method から取るか、 GetType からの Type.GetMethod で取るかで値が違う例:

using System.Reflection;

A a = (int x = 2) => { };

MethodInfo m1 = a.Method;
MethodInfo m2 = a.GetType().GetMethod("Invoke")!;

Console.WriteLine(m1.GetParameters()[0].DefaultValue); // 2
Console.WriteLine(m2.GetParameters()[0].DefaultValue); // 1

delegate void A(int x = 1);
ufcpp commented

型とラムダ式で違うデフォルト値指定できる。
型の方優先。
さすがに警告は出してくれる。

D d = (int x = 2) => x; // 警告出る。

Console.WriteLine(d()); // 1 の方になる。

delegate int D(int x = 1);