using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public delegate T Function<out T>(); //泛型委托 out关键字:基于协变的变体
//public delegate Strong Function2(); //普通委托
public delegate void Operate<in T>(T instance);//泛型委托 in关键字:基于逆变的变体
public class Program
{
static void Main(string[] args)
{
//协变委托
Function<Strong> funStrong = new Function<Strong>(GetInstance);
Function<Weak> funWeak = funStrong;
Weak weak = funWeak();
//逆变委托
Operate<Weak> opWeak = new Operate<Weak>(DoSth);
Operate<Strong> opStrong = opWeak;
opStrong(new Strong());
}
static Strong GetInstance()
{
return new Strong();
}
static void DoSth(Weak weak) {
//...
}
}
/// <summary>
/// 强类型
/// </summary>
class Weak { }
/// <summary>
/// 弱类型
/// </summary>
class Strong : Weak
{
}
}