[英]What is the exact meaning of delegate() method in C#?
I am pretty new in C# (I came from Java) and I am working on a SharePoint project. 我是C#的新手(我来自Java),正在从事SharePoint项目。
I have the following doubt related to this method in my code: 在我的代码中,我对此方法有以下疑问:
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
lock (this)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent;
DeleteExistingJob(JobName, parentWebApp);
});
}
catch (Exception ex)
{
throw ex;
}
}
}
as you can see this code is executed into delegate() {...} "block": 如您所见,此代码已执行到proxy(){...} “ block”中:
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent;
DeleteExistingJob(JobName, parentWebApp);
});
What exactly means this delegate() method? 这个委托方法到底是什么意思?
Reading here: https://docs.microsoft.com/it-it/dotnet/csharp/language-reference/keywords/delegate 在这里阅读: https : //docs.microsoft.com/it-it/dotnet/csharp/language-reference/keywords/delegate
it seems to me that it somethings like a way to declare an "anonymous" method where the implementation of this method is the code into the {...} block. 在我看来,这似乎是一种声明“匿名”方法的方法,其中该方法的实现是{...}块中的代码。
Is this the correct interpretation or am I missing something? 这是正确的解释,还是我缺少什么?
In case it is correct what is the pourpose of this delegate() method? 如果是正确的,那么这个委托方法的目的是什么? Why I am not declaring the code into a classic method? 为什么我不将代码声明为经典方法? What is it exact pourpose? 确切的目的是什么?
As per the documentation you referred to, the delegate
keyword is used for two purposes: 根据您引用的文档, delegate
关键字有两个用途:
Now you could write all the code in an anonymous method in a regular method and then use a method group conversion to create a delegate instance, but that can often be annoying - particularly if you want to use any local variables or parameters in the anonymous method. 现在,您可以在常规方法中以匿名方法编写所有代码,然后使用方法组转换来创建委托实例,但这通常很烦人-特别是如果您想在匿名方法中使用任何局部变量或参数。
So that's why you'd use an anonymous method - or in anything from C# 3 onwards, you're more likely to use a lambda expression instead. 因此,这就是为什么要使用匿名方法的原因-或从C#3开始的任何方法中,您更有可能使用lambda表达式 。
Consider how you'd have to create the delegate in your example if you didn't use an anonymous method or lambda expression. 考虑如果不使用匿名方法或lambda表达式,则必须在示例中创建委托的方式。 You'd need to write something like this: 您需要编写如下内容:
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
lock (this)
{
// Note: try/catch removed as it's pointless here, unless you're
// *trying* to obscure the stack trace in case of an exception
JobDeletionHelper helper = new JobDeletionHelper(properties);
// Note that we're using a method group conversion here - we're not
// invoking the method. We're creating a delegate which will invoke
// the method when the delegate is invoked.
SPSecurity.RunWithElevatedPrivileges(helper.DeleteJob);
}
}
// We need this extra class because the properties parameter is *captured*
// by the anonymous method
class JobDeletionHelper
{
private SPFeatureReceiverProperties properties;
internal JobDeletionHelper(SPFeatureReceiverProperties properties)
{
this.properties = properties;
}
public void DeleteJob()
{
// This is the code that was within the anonymous method
SPWebApplication parentWebApp = (SPWebApplication)properties.Feature.Parent;
DeleteExistingJob(JobName, parentWebApp);
}
}
If you're asking about the purpose of delegates themselves, that's a slightly bigger topic - but in a nutshell, it's the ability to represent executable code as an object, so it can be passed to other code to execute. 如果您要询问委托人自己的目的,那是一个稍大的话题-但总而言之,它是将可执行代码表示为对象的功能,因此可以将其传递给其他代码来执行。 (You can think of a delegate type as being like a single-method interface, if that's useful.) (如果有用,您可以将委托类型视为单方法接口。)
it seems to me that it somethings like a way to declare an "anonymous" method where the implementation of this method is the code into the {...} block. 在我看来,这似乎是一种声明“匿名”方法的方法,其中该方法的实现是{...}块中的代码。
Yes, that is on point! 是的,这很重要!
In case it is correct what is the pourpose of this delegate() method? 如果是正确的,那么这个委托方法的目的是什么? Why I am not declaring the code into a classic method? 为什么我不将代码声明为经典方法? What is it exact pourpose? 确切的目的是什么?
Since you mentioned that you came from Java you can think of passing a delegate(){ ... }
as passing an anonymous class in java to some extent. 既然您提到过您来自Java,那么您可以认为在某种程度上传递一个delegate(){ ... }
就是在Java中传递一个匿名类。
In Java, an anonymous class enables you to declare and instantiate a class at the same time. 在Java中,匿名类使您可以同时声明和实例化一个类。 They are like local classes except that they do not have a name . 它们就像本地类,只是它们没有名称 。 Use them if you need to use a local class only once . 如果只需要使用一次本地类,则使用它们。
For example, In Java prior to JDK8 you could do: 例如,在JDK8之前的Java中,您可以执行以下操作:
btn.setOnAction(new EventHandler<ActionEvent>() { //<--- implementation
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
or as of JDK8: 或自JDK8起:
btn.setOnAction(e -> System.out.println("Hello World!"));
Likewise in C# delegate () {...}
is allowing you to pass an implementation ie some behaviour on the fly. 同样,在C#中, delegate () {...}
允许您传递实现,即即时进行某些行为。
It was common pattern a while back to use delegate () {...}
but now you're most likely to use a lambda equivalent of () => { ... }
前一段时间使用delegate () {...}
是一种常见的模式,但是现在您最有可能使用等效于() => { ... }
的lambda
You may find these posts of interest: 您可能会发现以下有趣的帖子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.