kamagra how much to take

Take Advantage of Lambdas with RunWithElevatedPrivileges

One of the compelling features in C# 3.0 is the presentation of lambda expressions, cultivated out of the anonymous delegate (type-safe function pointers) support (don’t need to define a method somewhere else) that was present in C# 2.0. A lambda expression organizes with a left side composed of the arguments and a right expression which is the body of the method, split with a =>.

In the context of SPSecurity.RunWithElevatedPrivileges to encourage execution of a method with Full Control rights, it is common to see it as an anonymous method. For example, when using the SPUtility.SendEmail method it is ordinary to elevate rights. Using pre C# 3.0 mechanisms this would normally take on the form:

  1. SPSecurity.RunWithElevatedPrivileges(delegate
  2. {
  3. SPUtility.SendEmail(SPContext.Current.Site.OpenWeb(), false, false, "no_reply@sharepointsecurity.com", "this is my subject", "this is my body");
  4. });

This is a fair amount of real estate on the code surface. Wouldn’t it be nicer if we introduced a little bit of syntax sugar to tidy it up? This is where lambda expressions are accommodating. Taking the above method, this instead takes on the form:

  1. SPSecurity.RunWithElevatedPrivileges(() => SPUtility.SendEmail(SPContext.Current.Site.OpenWeb(), false, false, "no_reply@sharepointsecurity.com", "this is my subject", "this is my body"));

This is much cleaner, and required less typing!

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>