#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円で始められる本格的なお店のホームページ!

コメント:



(画像の文字列を入力して下さい)

トップ   編集 凍結 差分 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019/12/02 (月) 12:33:13 (1628d)

yVoC[UNLIMITȂ1~] ECirŃ|C Yahoo yV LINEf[^[Ōz500~`I


z[y[W ̃NWbgJ[h COiq 萔O~ył񂫁z COsیI COze