---
# System prepended metadata

title: mod12
tags: [C#]

---

```csharp=
void Add(int i, int j) => 
    Console.WriteLine(i + j);
void Sub(int i, int j) => 
    Console.WriteLine(i - j);
void Mul(int i, int j) => 
    Console.WriteLine(i * j);
void Div(int i, int j) => 
    Console.WriteLine(i / j);

//1.
delegate void MyDelegate(int i, int j);

//2.
MyDelegate compute = new MyDelegate(Add);

//3.
compute.Invoke(20, 4);
//========================================
//
//
//2.
MyDelegate compute = new MyDelegate(Add);
MyDelegate compute2 = Sub;
MyDelegate compute3 = Mul;


//3.
compute.Invoke(20, 4);
compute2.Invoke(20, 4);
compute3(20, 4);

//=====================================
//multicast
//2.
MyDelegate compute = Add;
compute += Sub;
compute += Mul;
compute += Div;

//3.
compute.Invoke(20, 4);
//=====================================
//?.
//2.
MyDelegate compute = Add;
compute -= Add;          

//3.
compute?.Invoke(20, 4);


void Add(int i, int j) =>
    Console.WriteLine(i + j);
void Sub(int i, int j) =>
    Console.WriteLine(i - j);
void Mul(int i, int j) =>
    Console.WriteLine(i * j);
void Div(int i, int j) =>
    Console.WriteLine(i / j);
//============================
delegate void Print(string s);

Print print=Console.WriteLine;
print.Invoke("xxxx");
//============================
//generic delegate
Action<string> print=Console.WriteLine;
print.Invoke("xxxx");


//2.
Action<int,int> compute = Add;

//===========================
DateTime GetTime()=>DateTime.Now;

Func<DateTime> time = GetTime;

Console.WriteLine(time());
//===========================
//event 
private void Form1_MouseMove(object sender, 
    MouseEventArgs e)
{
    Text = $"X:{e.X} Y:{e.Y}";
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{           

    if (checkBox1.Checked)
    {
        MouseMove += Form1_MouseMove;
    }
    else
    {
        MouseMove -= Form1_MouseMove;
    }
}
//===================================
class Account
{     
    public void PrintTransactions()
    {
        for (int i = 0; i <= 10; i++)
        {
            // 模擬查詢並列印交易明細...
            Thread.Sleep(300);           
        }
        Console.WriteLine("交易明細列印完畢!")
    }
}
Account acct = new Account();
Console.WriteLine("開始列印");
acct.PrintTransactions();
//==================================
class Account
{     
    //1.
    public event EventHandler<ProgressChangedEventArgs> ProgressChanged;

    public void PrintTransactions()
    {
        for (int i = 0; i <= 10; i++)
        {
            // 模擬查詢並列印交易明細...
            Thread.Sleep(300);

            //2.
            ProgressChanged?.Invoke(this, new ProgressChangedEventArgs(i*10,null));

        }
        Console.WriteLine("交易明細列印完畢!");
    }
}

internal class Program
{      

    static void Main(string[] args)
    {
        Account acct = new Account();

        //3.
        acct.ProgressChanged += Acct_ProgressChanged;
        Console.WriteLine("開始列印");
        acct.PrintTransactions();
    }
    //4.
    private static void Acct_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Console.WriteLine($"進度: {e.ProgressPercentage} %");
    }
}
//============================
//3.
//acct.ProgressChanged +=  delegate(object sender, ProgressChangedEventArgs e)
//{
//    Console.WriteLine($"進度: {e.ProgressPercentage} %");
//};

acct.ProgressChanged += delegate 
{
    Console.Write('.');
};
//====================================
//lambda expression
acct.ProgressChanged += (sender, e) =>
    Console.WriteLine($"進度: {e.ProgressPercentage} %");

```