首页 > 其他 > 详细

委托实例

时间:2015-05-07 18:29:15      阅读:180      评论:0      收藏:0      [点我收藏+]
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));
        }
    }
}

 

委托实例

原文:http://www.cnblogs.com/772933011qq/p/4485461.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!