一个委托的小例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateSample
{
public delegate void DelegateReportProgress(int progress);
class Program
{
static void Main(string[] args)
{
CalculateClass c = new CalculateClass();
c.CalculateReportMethod = new DelegateReportProgress(ReportCalculateProgress);
c.CalculateBigNum();
Console.WriteLine("我想换一种报告的样式我可以:");
c.CalculateReportMethod = new DelegateReportProgress(ReportCalculateProgress2);
c.CalculateBigNum();
Console.ReadLine();
}
static void ReportCalculateProgress(int progress)
{
Console.WriteLine("当前的计算进度是: "+progress.ToString());
}
static void ReportCalculateProgress2(int progress)
{
Console.WriteLine("当前的计算剩余的进度是: " + Convert.ToString(100 - progress));
}
}
public class CalculateClass
{
public DelegateReportProgress CalculateReportMethod;//定义一个方法指针
public void CalculateBigNum()
{
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 10000000; j++)
{
}
CalculateReportMethod(i);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateSample
{
public delegate void DelegateReportProgress(int progress);
class Program
{
static void Main(string[] args)
{
CalculateClass c = new CalculateClass();
c.CalculateReportMethod = new DelegateReportProgress(ReportCalculateProgress);
c.CalculateBigNum();
Console.WriteLine("我想换一种报告的样式我可以:");
c.CalculateReportMethod = new DelegateReportProgress(ReportCalculateProgress2);
c.CalculateBigNum();
Console.ReadLine();
}
static void ReportCalculateProgress(int progress)
{
Console.WriteLine("当前的计算进度是: "+progress.ToString());
}
static void ReportCalculateProgress2(int progress)
{
Console.WriteLine("当前的计算剩余的进度是: " + Convert.ToString(100 - progress));
}
}
public class CalculateClass
{
public DelegateReportProgress CalculateReportMethod;//定义一个方法指针
public void CalculateBigNum()
{
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 10000000; j++)
{
}
CalculateReportMethod(i);
}
}
}
}
本站文章除做特殊声明则一律属于原创,转载请注明出处
--周瑞喜(rain.zhou)
开源分享使人进步,使技术进步,使社会进步