代表优势 [英] Advantage of Delegates

查看:75
本文介绍了代表优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

public delegate int Calculate (int value1, int value2);


这是一个委托声明.我们正在使用像这样的委托对象来调用类方法

Calculate add = new Calculate(mc.add);
Calculate sub = new Calculate(mc.sub);



我们可以通过类对象调用此方法.但是为什么我们要使用此委托?
代表的优势是什么?请举例说明.


预先感谢,
Rajkumar.C

解决方案

它们是封装一段代码的好方法.例如,当您将事件处理程序附加到按钮时,该处理程序就是一个委托.该按钮不需要知道它的功能,只需要在正确的时间调用它即可.

另一个例子是LINQ-过滤,投影等都需要相同类型的模板代码.所做的所有更改都是代表过滤器,投影等的逻辑.使用C#3中的lambda表达式(将其转换为委托或表达式树),这真的很简单:

  var  namesOfAdults = people.Where(person = >  person.Age >  =  18 )
                          .Select(person = >  person.Name); 



(也可以将其表示为查询表达式,但不要与委托人相距太远.)

委托的另一种考虑方式是作为单方法接口类型.例如,EventHandler委托类型有点像:

 公共 接口 IEventHandler
{
    无效调用(对象发​​件人,EventArgs e)
} 



但是该框架中的委托支持允许将委托链接在一起,异步调用,用作事件处理程序等. /Articles/85296/6-important-uses-of-Delegates-and-Events>对委托人和事件的重要使用 [ ^ ]
代表的优势及其在C#.NET中事件中的作用 [ 公共 委托 void MulticastDelegate( int num); // 声明代表. class 实施 { 公共 int num1,num2; 实施() { num1 = 10 ; num2 = 5 ; } 公共 静态 void 添加( int num2) { // 应用一些逻辑 } 公共 静态 void 减去( int num1, int num2) { // 应用一些逻辑 } 公共 静态 void Main() { MulticastDelegate addDelgate = 新建 MulticastDelegate( add ); // 声明代理实例. addDelegate(Num1,Num2); // 调用委托以执行该功能. MulticastDelegate subDelegate = 新建 MulticastDelegate(subtract); // 声明委托的实例. subDelegate(Num1,Num2); // 调用委托以执行该功能. Console.Read(); } }


Hi all,

public delegate int Calculate (int value1, int value2);


This is a delegate declaration. We are calling class methods using delegate objects like this

Calculate add = new Calculate(mc.add);
Calculate sub = new Calculate(mc.sub);



we can call this methods through class objects. But why we use this delegates?
what are the advantages of delegates. Please explain with example.


Thanks in advance,
Rajkumar.C

They''re a great way of encapsulating a piece of code. For instance, when you attach an event handler to the button, that handler is a delegate. The button doesn''t need to know what it does, just how to call it at the right time.

Another example is LINQ - filtering, projecting etc all require the same kind of template code; all that changes is the logic to represent the filter, the projection etc. With lambda expressions in C# 3 (which are converted into delegates or expression trees) this makes it really simple:

var namesOfAdults = people.Where(person => person.Age >= 18) 
                          .Select(person => person.Name); 



(That can also be represented as a query expression, but let''s not stray too far from delegates.)

Another way of thinking of a delegate is as a single-method interface type. For example, the EventHandler delegate type is a bit like:

public interface IEventHandler 
{ 
    void Invoke(object sender, EventArgs e) 
} 



But the delegate support in the framework allows delegates to be chained together, invoked asynchronously, used as event handlers etc.


look at important uses of Delegates and Events[^]
Advantage of Delegates and their role in Events in C# .NET[^]


Delegates are like providing reference to the function at run time.
Suppose we have various methods that follow a delegate and that we need to call those methods on some event or action ,in such scenario we need to use the delegate .Multicast delegates can be handy where we need to provide separate instance for each of the calling methods.


EXAMPLE ::->

public delegate void MulticastDelegate(int num);// Declaring a delegate..

class Implement
{
   public int num1,num2;
   Implement()
   {
    num1=10;
    num2=5;
   }
    public static void add(int num1,int num2)
      {  
     //Apply some Logic 
      }
    public static void subtract(int num1,int num2)
     {
      //Apply some Logic
     }
   public static void Main()
   {
     MulticastDelegate addDelgate= new MulticastDelegate(add);//Declare an instance of the Delegate..
     addDelegate(Num1,Num2);//Calling the Delegate to perform the Function.
     MulticastDelegate subDelegate= new MulticastDelegate(subtract);//Declare an instance of the Delegate..
     subDelegate(Num1,Num2);//Calling the Delegate to perform the Function.
     Console.Read();
   }

}


这篇关于代表优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆