delegateの読み方

delegateの読み方

C#や.NET、Swift等で使用される英単語「delegate」の読み方を掲載してます。

読み⽅

デリゲート」と読みます。

英訳

「代議士、委譲」という意味があります。

delegateとは

C#だと関数の型を定義して、動的に処理を変える事が可能です。

下記はC#での、delegateの使用例となります。

using System;

namespace testapp
{
    // delegate宣言
    public delegate int DelegateTest(int a, int b);
    
    class Program
    {
        static void Main(string[] args)
        {
            DelegateTest td;
            int x = 1;

            //条件により分岐
            if (x == 0) { 
                td = TestA;
            }
            else { 
                td = TestB;
            }

            Console.WriteLine(td(1, 2));
        }
        static int TestA(int x, int y)
        {
            return x + y;
        }

        static int TestB(int x, int y)
        {
            return x * y;
        }


    }
}

実行結果

2