Code Inspection: Lambda expression/anonymous method should not have captures of the containing context
Lambda expressions in C# are usually associated with some performance and memory impact. Although the abstraction that lambda expression provide is worth additional costs in most scenarios, these costs may not be acceptable for some hot path code.
To distinguish such code, you can use the [RequireStaticDelegate]
attribute from JetBrains.Annotations. This attribute enforces allocation-less coding practices by reporting captures of the containing context in lambda expressions passed to parameters annotated with this attribute. Lambda expressions without any captures of the containing context only allocate the delegate instance once and cache it for the rest of the program execution, so such lambda expressions become allocation-less.
Here is an example of an API that expects users to compute the cached value using the Func
delegate type input parameter.
... but it's too easy to make a mistake and capture the variable key
instead of x
parameter:
JetBrains Rider will report such captures, but it will not suggest any quick-fixes — you need to make sure that your code works properly without capturing anything from the invocation context.
If you pass a method as the delegate parameter, JetBrains Rider will suggest adding static
modifier to the lambda expression when possible to prevent captures at the compiler level: