简体   繁体   English

此示例使用委托()的确切含义是什么?

[英]What is the exact meaning of this example using delegate()?

I am not so into C# (I came from Java) and I have the following doubts about how exactly works the delegate methods related to this example: 我对C#不太了解(我来自Java),对于与该示例相关的委托方法的工作方式,我有以下疑问:

List<string> urlList = IndirizziProtocolliSQL.GetListaIndirizziSiti(dbConfig);

foreach (string currentUrl in urlList)
{
    Debug.Print("Current url: " + currentUrl);

    SPSecurity.RunWithElevatedPrivileges(delegate ()
    {
        using (SPSite oSiteCollection = new SPSite(currentUrl))
        {
            using (SPWeb oWebsite = oSiteCollection.OpenWeb())
            {
            }
        }
    });
}

From what I can understand reading the official documentation: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/ 根据我的理解,阅读官方文档: https : //docs.microsoft.com/zh-cn/dotnet/csharp/programming-guide/delegates/

the delegate() is used to pass a method as input parameter of another method. 委托()用于传递一个方法作为另一个方法的输入参数。

For example if I have something like: 例如,如果我有类似的东西:

public delegate int MyDelegate (string s);

it means is a reference to any method having the signature of this method (return type, method name, in put parameters). 它表示对具有该方法签名的任何方法的引用(返回类型,方法名称,放入参数)。

If it is correct, what exactly means my first original example? 如果正确,那第一个原始示例到底意味着什么? Why instead a method signature I have a using(...){...} block? 为什么我有一个using(...){...}块呢?

What is the exact meaning of this syntax? 该语法的确切含义是什么?

The delegate () { } just indicates an anonymous inline method / delegate is passed into the function. delegate () { }仅表示一个匿名内联方法/委托已传递到函数中。 The body of that method is just like any C# code block, and can contain using statements, or any other statement. 该方法的主体就像任何C#代码块一样,可以包含using语句或任何其他语句。

This would be similar to: 这类似于:

private void Method()
{
    using (SPSite oSiteCollection = new SPSite(currentUrl))
    {
        using (SPWeb oWebsite = oSiteCollection.OpenWeb())
        {
        }
    }
});

SPSecurity.RunWithElevatedPrivileges(Method);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM