一个委托的小例子

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);
            }
        }
    }
}

你可能感兴趣的:(例子)