代表的基本用途是什么?什么时候用? [英] What is the basic use of Delegates? When to use it?

查看:105
本文介绍了代表的基本用途是什么?什么时候用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



在阅读代表的同时,我坚持了一点为什么我们使用代表 ??



我知道委托被用作函数的引用,用于在运行时而不是编译时调用函数。



但我仍然无法理解为什么我们使用...



它是否会提高速度?



现在我有一段代码:



 < span class =code-keyword> public   delegate   double  Delegate_Prod( int  a, int  b); 
class Class1
{
static int fn_Prodvalues( int val1, int val2)
{
return val1 * val2;
}

静态 void Main( string [] args)
{
Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
double res = delObj( 5 6 );
Console.WriteLine( 结果:,+ res);
Console.ReadLine();
}
}





但这可以直接通过调用类对象来完成...

解决方案





看看这里:

C#代表:一步一步 [ ^ ]

了解C#中的代表 [ ^ ]

使用事件和代理 [ ^ ]

代表和事件的6个重要用途 [ ^ ]

C#中的代表 - 尝试查看内部 - 第1部分 [ ^ ]

C#中的代表 - 尝试向内看 - 第2部分 [ ^ ]

C#中的代表 - 尝试查看内部 - 部分3 [ ^ ]

C#中的代表 - 尝试向内看。第4部分 [ ^ ]

C#中的代表 - 尝试向内看:第5部分 [ ^ ]

如何在C#中使用委托 - 步骤1 [ ^ ]


请查看我过去的答案:

代表的额外优势是什么 [ ^ ],

C#.NET中代理人的优势是什么? [ ^ ]。



另见这两个答案:

关于代表的混淆 [ ^ ],

在N#中传递一个以N参数作为参数的方法 [ ^ ]。





Atul Khanduri写道:

现在我有另一个简单的问题,或者更确切地说需要一个简单的例子来说明在参数中有委托的函数。

没什么好看的,但是我认为它会你最好先编写自己的函数然后问一个问题是否卡住了。



我记得我已经写过这样的例子了。请看这里:传递方法使用N参数作为C#中的参数 [ ^ ](我已经在上面引用了这个答案;代码示例已经完成)。



-SA


参见本教程 [ ^ ]和这个 [ ^ ]。

Hello all,

While reading about delegates, i stucked in 1 point that is "why we use delegates"??

I know delegates are used as a reference to function and used to call functions at run-time rather than compile-time.

But still i am not able to understand why exactly we use...

Is it increases speed??

Okay now i have a piece of code:

public delegate double Delegate_Prod(int a,int b);
class Class1
{
    static int fn_Prodvalues(int val1,int val2)
    {
        return val1*val2;
    }

    static void Main(string[] args)
    {
        Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues);
        double res = delObj(5,6);
        Console.WriteLine ("Result : ",+res);
        Console.ReadLine();
    }
}



But this can be done directly by calling class object also...

解决方案

Hi,

Have a look here:
C# Delegates: Step by Step[^]
Understanding Delegates in C#[^]
Using Events and Delegates in C#[^]
6 important uses of Delegates and Events[^]
Delegates in C# - Attempt to Look Inside - Part 1[^]
Delegates in C# - Attempt to Look Inside - Part 2[^]
Delegates in C# - Attempt to Look Inside - Part 3[^]
Delegates in C# - Attempt to look inside. Part 4[^]
Delegates in C# - Attempt to look inside: Part 5[^]
How to use delegates in C# - step 1[^]


Please see my past answers:
What is the Extra Advantage of Delegate[^],
What are the advantages of delegates in C#.NET?[^].

See also these two answers:
Confusion about delegates[^],
Pass a method with N params as parameter in C#[^].

[EDIT]

Atul Khanduri wrote:

Now I have another simple question or rather need a simple example for a function having delegates in their parameter.

Nothing silly about it, but I thought it would be better for you to write your own function first and ask a question if you stuck.

I remember I already wrote such examples. Look here: Pass a method with N params as parameter in C#[^] (I already referenced this answer above; the code sample is complete).

—SA


See this tutorial[^], and this one[^].


这篇关于代表的基本用途是什么?什么时候用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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