在C#中进行委托.其目的是什么? [英] delegate in c# .what its purpose?
本文介绍了在C#中进行委托.其目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
C#编译器将委托公开为引用类型,您无法使用new运算符实例化委托.我们无法使用新关键字实例化委托.
请解决
thanx
the C# compiler exposes delegates as reference types, you could not instantiate the delegate by using the new operator.we cannot instantiate a delegate by using new key word.
please resolve it
thanx
推荐答案
将其搜索为"Delegate in c#",您将不会获得任何结果.好的文章,将帮助您理解概念. :)
Google it for "Delegate in c#" and you will get no. of good articles which will help you to understand the concept. :)
实际上已委派了代表,例如,参见: [ ^ ].
Actually delegates are istantiated, see, for instance: "Delegates Tutorial at MSDN"[^].
委托不是实例,而是代表对方法的引用的变量.
结果,您无法使用new
运算符来实例化它:这意味着您正在创建一个新方法,这显然很愚蠢!
如果您将委托视为方法的指针",那么您就在那里.这是稍后访问方法而无需知道其名称的一种方法.
A delegate is not an instance - it is a variable holding a reference to a method.
As a result, you cannot instantiate it by using thenew
operator: that would imply that you are creating a new method, which is plainly silly!
If you think of a delegate as a "Pointer to a method" then you are there. It is a way to access a method later without needing to know it''s name at that point.
public string GetScreenName() { return "Screen"; }
public string GetPrinterName() { return "Printer"; }
public delegate string GetNameMethod();
public GetNameMethod GetName;
...
if (isUsingScreen)
{
GetName = GetScreenName;
}
else
{
GetName = GetPrinterName;
}
Console.WriteLine(GetName());
这篇关于在C#中进行委托.其目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文