委托_lambda表达式


01.委托作为方法传递

委托_lambda表达式_02

using System;
using static System.Console;

namespace Mydelegate1
{
//委托声明
public delegate bool ComparsionHandler(int first,int second);
class Program
{
static void Main(string[] args)
{
int[] sum = new[] {5, 10, 2, 0, 40, 6, 9, 8};

BubbleSort(sum, GreaterThan);
foreach (var item in sum)
{
WriteLine(item);
}
ReadKey();
}

//委托作为方法传递
public static void BubbleSort(int[] items,ComparsionHandler comparsionMethod)
{
int i ;
int j ;
int temp;

for (i = items.Length - 1; i >=0; i--)
{
for (j = 1; j <=i ; j++)
{
if (comparsionMethod(items[j-1],items[j]))
{
temp = items[j - 1];
items[j - 1] = items[j];
items[j] = temp;
}
}
}
}

public static bool GreaterThan(int first, int second)
{
return first > second;

}
}

}

委托_运算符_03

委托_lambda表达式_04


02.

委托_运算符_05


委托_运算符_06


委托_lambda表达式_07


03.Lambda表达式

委托_lambda表达式_08

//委托声明
public delegate bool ComparsionHandler(int first, int second);
class Program
{
static void Main(string[] args)
{

int[] sum = new[] { 5, 10, 2, 0, 40, 6, 9, 8 };
//整数 first和整数 second 用于 返回 first大于second的结果
BubbleSort(sum, (int first, int second) => { return first >second;});
//BubbleSort(sum, (int first, int second) => first > second);
foreach (var item in sum)
{
WriteLine(item);
}

ReadKey();
}


//委托作为方法传递
public static void BubbleSort(int[] items, ComparsionHandler comparsionMethod)
{
int i;
int j;
int temp;

for (i = items.Length - 1; i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (comparsionMethod(items[j - 1], items[j]))
{
temp = items[j - 1];
items[j - 1] = items[j];
items[j] = temp;
}
}
}
}

}

委托_运算符_09


委托_lambda表达式_10


04.通用的委托

委托_lambda表达式_11

namespace Mydelegate5
{
public delegate void Action<T>(T arg);

class Program
{
public Func<int,float> func;
static void Main(string[] args)
{
Program p=new Program();
p.func = Sum;
WriteLine(p.func(10)); //10
ReadKey();
}

static float Sum(int a)
{
return a;
}
}
}

委托_运算符_12


05.多播委托

和空值传播运算符

using System;
using static System.Console;
namespace Event02
{
class Program
{
static void Main(string[] args)
{
Thermostat thermostat=new Thermostat();
Cooler cooler=new Cooler(60);
Heater heater=new Heater(80);

thermostat.OnTemperatureChanged += cooler.OnTemperatureChanged;
thermostat.OnTemperatureChanged += heater.OnTemperatureChanged;
while (true)
{
thermostat.CurrentTemperature = float.Parse(ReadLine());
}

ReadKey();
}
}

//发布者
class Thermostat
{
public Action<float> OnTemperatureChanged { get; set;}
private float _CurrentTemperature;

public float CurrentTemperature
{
get => _CurrentTemperature;
set
{
if (value!=CurrentTemperature)
{
_CurrentTemperature = value;
OnTemperatureChanged?.Invoke(value);
}
}
}
}


//加热器
class Cooler
{
public float Temperature { get; set; } //温度
public Cooler(float temperature)
{
this.Temperature = temperature;
}

public void OnTemperatureChanged(float newTemperature)
{
if (newTemperature>Temperature)
{
WriteLine("Cooler:ON");
}
else
{
WriteLine("Cooler:Off");
}
}
}

//冷却器
class Heater
{
public float Temperature { get; set; } //温度
public Heater(float temperature)
{
this.Temperature = temperature;
}
public void OnTemperatureChanged(float newTemperature)
{
if (newTemperature <Temperature)
{
WriteLine("Heater:ON");
}
else
{
WriteLine("Heater:Off");
}
}
}
}