Func<TResult>デリゲート パラメーターを受け取らずに、TResult パラメーターに指定された型の値を返すメソッドをカプセル化します。 TResult:このデリゲートによってカプセル化されるメソッドの戻り値。
Func 汎用デリゲートの最後のジェネリック型パラメーターは、デリゲート シグネチャの戻り値の型を指定します。 他のジェネリック型パラメーターは反変 (in キーワード) ですが、この最後のジェネリック型パラメーターは共変 (out キーワード) です。 using System; using System.IO; public class TestDelegate { delegate bool WriteMethod(); public static void Main() { OutputTarget output = new OutputTarget(); //Delegate使用 //WriteMethod methodCall = output.SendToFile; //Func<TResult> デリゲートをインスタンス化する Func<bool> methodCall = output.SendToFile; //匿名メソッド //Func<bool> methodCall = delegate() { return output.SendToFile(); }; //ラムダ式 //Func<bool> methodCall = () => output.SendToFile(); if (methodCall()) Console.WriteLine("Success!"); else Console.WriteLine("File write operation failed."); } } public class OutputTarget { public bool SendToFile() { try { string fn = Path.GetTempFileName(); StreamWriter sw = new StreamWriter(fn); sw.WriteLine("Hello, World!"); sw.Close(); return true; } catch { return false; } } } 超多機能のアクセス解析が永久無料! コメント: Func |