C# delegate

一、委托

当我们需要把方法做为参数传递给其他方法的时候,就需要使用委托。

因为有时候,我们要操作的对象,不是针对数据进行的,而是针对某个方法进行的操作。

       我们还是来以代码入手

  


using System;
namespace gosoa.com.cn
{
    
public class test
    {
        
public delegate string GetAString();
        
public static void Main()
        {
            
int x=10;
            GetAString firstString
=new GetAString(x.ToString);
            Console.WriteLine(firstString());
            
//上句和下面这句类似。
            
//Console.WriteLine(x.ToString()); 
        }
    }
}

 

    在上例中,public delegate string GetAString(); 就是声明了一个委托(delegate),其语法和方法的定义类似,只是没有方法体,前面要加上关键字 delegate 。定义一个委托,基本上是定义一个新类,所以,可以在任何定义类的地方,定义委托。

注意,在C#中,委托总是自带一个有参数的构造函数,这就是为什么在上例中,GetAString firstString=new GetAString(x.ToString); 通过这句初始化一个新的delegate的时候,给传递了一个x.ToString 方法。但,在定义delegate的时候,却没有定义参数。

      

在看另一个例子之前,我们先来了解下匿名方法。

       匿名方法的使用,我们看个例子

 


using System;
namespace gosoa.com.cn
{
    
public class test
    {
        
delegate string GetUrl(string val);
        
static void Main(string [] args)
        {
            
string domin="www.gosoa.com.cn";
            GetUrl url
=delegate(string  param)
            {
                 param
="http://"+param;
                 
return param;
            };
            Console.WriteLine(url(domin)); 
        }
    }
}

 

在本例中,GetUrl url=delegate(string  param) 在这里实例化一个delegate的时候,采用了匿名的方法。本例输出的结果是 http://www.gosoa.com.cn

 

接下来我们再看一个委托的例子。 

 


using System;
namespace gosoa.com.cn
{
    
class NumberOpthion
    {
        
public static double numOne(double x)
        {
            
return x*2;
        }
        
public static double numTwo(double x)
        {
            
return x*x;
        }
    }

    
public class delegateTest
    {
        
        
delegate double DoubleOpration(double x);
        
static void printNumber(DoubleOpration dp,double x)
        {
            
double result=dp(x);
            Console.WriteLine(
                
"value is {0}, result of DoubleOpration is {1}:",x,result    
            );
        }

        
static void Main()
        {
             DoubleOpration doption 
=new DoubleOpration(NumberOpthion.numOne);    
             printNumber(doption,
1.5);
             doption 
=new DoubleOpration(NumberOpthion. numTwo);    
             printNumber(doption,
3.2);

        }
    }
}

 

首先我们定义了一个NumberOpthion类。用来对数字进行*22次方运算。接着,我们定义了一个委托delegate double DoubleOpration(double x)。下面,我们定义了printNumber(DoubleOpration dp,double x) 这样一个方法,其中一个参数就是委托。最后我们DoubleOpration doption =new DoubleOpration(NumberOpthion.numOne);实例化了一个委托,并调用了 printNumber 方法。最后的输出结果是

Value is 0.5  result of DoubleOpration is 3;

Value is 3.2  result of DoubleOpration is 10.24;

 

在上例中,我们如果采用匿名方法,代码就会如下: 

 


using System;
namespace gosoa.com.cn
{
    
public class delegateTest
    {
        
delegate double DoubleOpration(double x);
        
static void printNumber(DoubleOpration dp,double x)
        {
            
double result=dp(x);
            Console.WriteLine(
                
"value is {0}, result of DoubleOpration is {1}:",x,result    
            );
        }
        
static void Main()
        {
             DoubleOpration doptionOne 
=delegate(double x){return x*2;};
             DoubleOpration doptionTwo 
=delegate(double x){return x*x;};
             printNumber(doptionOne,
1.5);
             printNumber(doptionTwo,
3.2);
        }
    }
}

 

委托,还有一种情况,是多播委托。这个在以后我们应用到的时候,会学习到。

posted @ 2009-10-24 00:00  neilxiang  阅读(520)  评论(0编辑  收藏  举报