委托实例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 委托
{
    //随机数产生委托类型
    delegate int DRandomBuilder(int min,int max);
    class CRandomBuilder 
    {
        private Random rnd;
        public CRandomBuilder()
        {
            rnd = new Random();
        }
        public int GetRandomNumber1(int min, int max)
        {
            return rnd.Next(min,max+1);
        }
        public int GetRandomNumber2(int min, int max)
        {
            Random r = new Random(DateTime.Now.Millisecond);
            return r.Next(min,max+1);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            CRandomBuilder bb = new CRandomBuilder();
            DRandomBuilder dr1 = new DRandomBuilder(bb.GetRandomNumber1);
            DRandomBuilder dr2 = new DRandomBuilder(bb.GetRandomNumber2);
            Console.WriteLine("第一个随机数是{0}",dr1(1,9));
            Console.WriteLine("第二个随机数是{0}",dr2.Invoke(100,900));
        }
    }
}
原文地址:https://www.cnblogs.com/772933011qq/p/4485461.html