2

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);
        }
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.