class Program
{
//定义委托
delegate bool Function(int num);
//定义委托逻辑
static Function GreaterThan10 = delegate(int n){ return n >= 10; };
//使用委托的方法
static List<int> Traverse(List<int> nums,Function function
) {
var list = new List<int>();
foreach (var num in nums) {
if (function(num)) {
list.Add(num);
}
}
return list;
}
static void Main(string[] args)
{
//调用委托
List<int> nums=Traverse(new List<int>() { 1,2,3,4,5,6,7,11}, GreaterThan10);
foreach (int num in nums) {
Console.WriteLine(num);
}
Console.Read();
}
}
定义委托逻辑
static Function GreaterThan10 = delegate(int n){ return n >= 10; };
可以使用lambda在调用方法时进行简化,如
List nums=Traverse(new List() { 1,2,3,4,5,6,7,11}, n=>n>=10);
Action 没有返回值的delegate(委托)
Func<输入参数,返回值> 有返回值的delegate(委托)
Predicate 返回值布尔的delegate(委托)
多播委托
方法签名一致的委托可以进行加减,委托会采用链表的方式顺序执行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
//用户类
class Person {
public string name { get; set; }
public Newspaper newspaper{set;get;}
public Person(string name) { this.name = name; }
public void setNewpaper(Newspaper newspaper) {
this.newspaper = newspaper;
}
public void ReadNewpaper() {
Console.WriteLine("个人{0}正在读报纸,出版社是{3},标题是{1},内容是{2}",this.name,this.newspaper.Title,this.newspaper.Content,this.newspaper.PublisherName);
}
}
//公司类
class Company
{
public string name { get; set; }
public Newspaper newspaper { set; get; }
public Company(string name) { this.name = name; }
public void setNewpaper(Newspaper newspaper)
{
this.newspaper = newspaper;
}
public void ReadNewpaper()
{
Console.WriteLine("公司{0}正在读报纸,出版社是{3},标题是{1},内容是{2}", this.name, this.newspaper.Title, this.newspaper.Content, this.newspaper.PublisherName);
}
}
class Publisher {
public string Name { set; get; }
public Publisher(string name) { this.Name = name; }
//public delegate void _Subscribers(Newspaper newspaper);
//public _Subscribers Subscribers { set; get; }
public Action<Newspaper> Subscribers;
//public List<Person> Persons = new List<Person>();
public void SendNewspaper(Newspaper newspaper)
{
newspaper.PublisherName = Name;
if (Subscribers!=null) {
Subscribers(newspaper);
}
}
}
//报纸类
class Newspaper {
public string PublisherName { set; get;}
public string Title { set; get; }
public string Content { set; get; }
}
class Program
{
static Action<int> Print;
static void Main(string[] args)
{
var publisher = new Publisher("出版社");
var A = new Person("a");
var B = new Person("b");
var C = new Person("c");
var D = new Company("d");
publisher.Subscribers = A.setNewpaper;
publisher.Subscribers += B.setNewpaper;
publisher.Subscribers += C.setNewpaper;
publisher.Subscribers += D.setNewpaper;
publisher.SendNewspaper(new Newspaper() { Title = "报纸标题",Content = "报纸内容"});
D.ReadNewpaper();
Console.ReadLine();
}
}
}
在出版社类Publisher 中有SendNewspaper报纸派发方法和Subscribers委托。。SendNewspaper方法中调用委托,在进行报纸派发的时候。
用户和公司存在方法setNewpaper,setNewpaper方法签名与Subscribers一致固能使用委托,且可使用多播委托(链表),之后出版社类Publisher调用SendNewspaper方法处理委托
多播链表异常
if (Subscribers!=null) {
Subscribers(newspaper);
}
这一段代码如果在链表执行时出现异常链表不会继续执行下去,固需要修改为:
if (Subscribers!=null) {
foreach (Action handler in Subscribers.GetInvocationList()) {
try
{
handler(newspaper);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
//Subscribers(newspaper);
}
存在多个出版社Publisher1,Publisher2的情况下,可以EventHandler Subscribers定义事件(EventHandler等价于 Action<this,PublisherArgs>),
Subscribers判断属于哪一家出版社
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class PublisherArgs : System.EventArgs {
public Newspaper newspaper { set; get; }
public PublisherArgs(Newspaper newspaper) { this.newspaper = newspaper; }
}
//用户类
class Person
{
public string name { get; set; }
public Newspaper newspaper { set; get; }
public Person(string name) { this.name = name; }
public void setNewpaper(Object sender, PublisherArgs args)
{
if (sender is Publisher1) {
args.newspaper.PublisherName = (sender as Publisher1).Name;
}
if (sender is Publisher2)
{
args.newspaper.PublisherName = (sender as Publisher2).Name;
}
this.newspaper = args.newspaper;
}
public void ReadNewpaper()
{
Console.WriteLine("个人{0}正在读报纸,出版社是{3},标题是{1},内容是{2}", this.name, this.newspaper.Title, this.newspaper.Content, this.newspaper.PublisherName);
}
}
//公司类
class Company
{
public string name { get; set; }
public Newspaper newspaper { set; get; }
public Company(string name) { this.name = name; }
public void setNewpaper(Object sender, PublisherArgs args)
{
if (sender is Publisher1)
{
args.newspaper.PublisherName = (sender as Publisher1).Name;
}
if (sender is Publisher2)
{
args.newspaper.PublisherName = (sender as Publisher2).Name;
}
this.newspaper = args.newspaper;
}
public void ReadNewpaper()
{
Console.WriteLine("公司{0}正在读报纸,出版社是{3},标题是{1},内容是{2}", this.name, this.newspaper.Title, this.newspaper.Content, this.newspaper.PublisherName);
}
}
class Publisher1
{
public string Name { set; get; }
public Publisher1(string name) { this.Name = name; }
//public delegate void _Subscribers(Newspaper newspaper);
//public _Subscribers Subscribers { set; get; }
public event EventHandler<PublisherArgs> Subscribers;
//public List<Person> Persons = new List<Person>();
public void SendNewspaper(Newspaper newspaper)
{
if (Subscribers != null)
{
foreach (EventHandler<PublisherArgs> handler in Subscribers.GetInvocationList())
{
try
{
handler(this, new PublisherArgs(newspaper));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}
class Publisher2
{
public string Name { set; get; }
public Publisher2(string name) { this.Name = name; }
//public delegate void _Subscribers(Newspaper newspaper);
//public _Subscribers Subscribers { set; get; }
public event EventHandler<PublisherArgs> Subscribers;
//public List<Person> Persons = new List<Person>();
public void SendNewspaper(Newspaper newspaper)
{
if (Subscribers != null)
{
foreach (EventHandler<PublisherArgs> handler in Subscribers.GetInvocationList())
{
try
{
handler(this, new PublisherArgs(newspaper));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}
//报纸类
class Newspaper
{
public string PublisherName { set; get; }
public string Title { set; get; }
public string Content { set; get; }
}
class Program
{
static Action<int> Print;
static void Main(string[] args)
{
var publisher = new Publisher1("出版社");
var A = new Person("a");
var B = new Person("b");
var C = new Person("c");
var D = new Company("d");
publisher.Subscribers += A.setNewpaper;
publisher.Subscribers += B.setNewpaper;
publisher.Subscribers += C.setNewpaper;
publisher.Subscribers += D.setNewpaper;
publisher.SendNewspaper(new Newspaper() { Title = "报纸标题", Content = "报纸内容" });
D.ReadNewpaper();
Console.ReadLine();
}
}
}