Also, if I declare string name outside the if block, resharper says Access to modified closure, but does it matter in this case?
The code will still work despite the fact that your code generates this warning. This happens to not be a problem unless you modify the name
variable some time before you actually invoke that closure. In the code you've shown you never use it again, so it wouldn't be a problem, but if you ever modified it in code shown after the code you have it would be a problem.
Why do you want to do this in the first place? For starters, I don't really see the point of the variable to begin with. Why not just write: x => x.name == args["name"]
? If you did want a local variable for this, why would you want to scope it at the higher level? You should always scope any variable at the lowest scope possible while still allowing it to be accessed where it needs to be. You shouldn't be modifying it outside the if
, so don't expose it outside the if
.
I am new to PredicateBuilder, so if someone an explain what PredicateBuilder.False() means as opposed to PredicateBuilder.True()
It creates an expression that resolves to false
if invoked instead of true
...
Note that false || variable
is equivalent to just variable
. The reason you want to initialize it to false is to avoid special-casing the first actual check you want by initializing the PredicateBuilder
there. It allows each of your variables to be consistent and always OR
whatever it is that the predicatebuilder currently resolves to.
what does the Compile method do?
It compiles the expression into a delegate that can be invoked.