Question

Here two methods below that uses SPSecurity.RunWithElevatedPrivileges(). I just want to know the difference between two?

Method 1:

public void Method1(SPUser user)
        {
            if (RequireImpersonation == true)
                SPSecurity.RunWithElevatedPrivileges(delegate() { PerformRemoveFromVisitorsGroup(user); });
            else
                PerformRemoveFromVisitorsGroup(user);
        }

Mehtod 2:

public void Method2(SPUser user)
        {
            if (RequireImpersonation == true)
                SPSecurity.RunWithElevatedPrivileges(() => PerformRemoveFromVisitorsGroup(user));
            else
                PerformRemoveFromVisitorsGroup(user);
        }
Was it helpful?

Solution

Once the code is compiled, there is no difference between these two. In this case - delegate { } is equal to () => { }. There are a couple of things, but they are not relevant in this case:

  1. If you assign the lambda to a delegate type (such as Func or Action) you'll get an anonymous delegate. [1]
  2. If you assign the lambda to an Expression type, you'll get an expression tree instead of a anonymous delegate. The expression tree can then be compiled to an anonymous delegate. [1]

[1] - http://weblogs.asp.net/scottgu/new-orcas-language-feature-lambda-expressions

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top