#author("2019-07-26T10:53:34+08:00","default:Admin","Admin") CSharp Action<T>デリゲート 単一のパラメーターを受け取り、戻り値を持たないメソッドをカプセル化します。 T:このデリゲートによってカプセル化されるメソッドのパラメーターの型。 using System; using System.Windows.Forms; delegate void DisplayMessage(string message); public class TestCustomDelegate { public static void Main() { //デリゲート利用 DisplayMessage messageTarget; //Action<T> デリゲートをインスタンス化する Action<string> messageTarget; if (Environment.GetCommandLineArgs().Length > 1) //匿名メソッド利用 //messageTarget = delegate(string s) { ShowWindowsMessage(s); }; //ラムダ式利用 //messageTarget = s => ShowWindowsMessage(s); messageTarget = ShowWindowsMessage; else messageTarget = Console.WriteLine; messageTarget("Hello, World!"); } private static void ShowWindowsMessage(string message) { MessageBox.Show(message); } } Method as Parameter †using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ActionFuncTest { class Program { static int num = 0; static void Main(string[] args) { Func<bool> CanEnd = () => num >= 5; Action Method = () => { num += 1; Console.WriteLine("show"); }; Action EndMethod = () => Console.WriteLine("ShowEnd"); StartYourShow(CanEnd, Method, EndMethod); Console.Read(); } static void StartYourShow(Func<bool> CanEnd, Action Method, Action EndMethod) { while (!CanEnd()) Method(); EndMethod(); } } } 月額たった 1,050円で始められる本格的なお店のホームページ! コメント: |