Will this fail ?
Resharper reports this as an instance of "Access to modified Closure"
Will the lambda be triggered for every value ? Is the iterator generating the complete list of all interval values in this
before the line that changes first
runs? Or is the line first = itvl;
running for reach iteration, and that changed value of first
used for subsequent iterations ??
public HourInterval FirstInterval
{
get
{
var first = HourInterval.Make(DateTime.MaxValue);
foreach (var itvl in this.Where
(itvl => itvl < first))
first = itvl;
return first;
}
}
NOTE. HourInterval
is a value-type struct that represents each one-hour-long calendar h开发者_如何学编程our... and this
is an IEnumerable
collection of HourInterval
objects
EDIT:
The above is what Resharper suggested to convert to a LINQ expression from the below foreach
construction ...
public HourInterval FirstInterval
{
get
{
var first = HourInterval.Make(DateTime.MaxValue);
foreach (var itvl in this)
if(itvl < first)
first = itvl;
return first;
}
}
OK, this is a bit of a mess.
First off, it is a poor programming practice to use the same variable name in two slightly inconsistent ways in the same code. It's very confusing. Frankly, I would prefer this to be illegal; the reason that it is not illegal is a bit complicated; see http://blogs.msdn.com/b/ericlippert/archive/2009/11/05/simple-names-are-not-so-simple-part-two.aspx for details.
Let's get rid of that problem:
var first = HourInterval.Make(DateTime.MaxValue);
foreach (var itvl in this.Where(x => x < first))
first = itvl;
Now, the next question is: is Resharper correct to note that this is an access to a modified closure? Yes, Resharper is correct; you are modifying a closed-over variable of a lambda that will be called repeatedly. Resharper is noting that this is dangerous because Resharper does not know what "Where" does. For all Resharper knows, "Where" is caching every predicate it gets and is saving it up to execute later, in the mistaken belief that each predicate will do something different. In fact each predicate is the same, because each predicate is closed over the same variable, not closed over different variables.
Clearly no sensible implementation of "Where" will do that. But Resharper doesn't know that.
The next question is: is this a sensible thing to do? No. This is a terribly unidiomatic and confusing way to implement "Min", by modifying the closed-over variable of a predicate to "Where". If you want to write Min, just write Min:
static DateTime? Min(this IEnumerable<DateTime> seq)
{
DateTime? min = null;
foreach(DateTime current in seq)
{
if (min == null || current < min.Value)
min = current;
}
return min;
}
There, that returns the earliest date in the sequence, or null if the sequence is empty. No messing about with Where and predicates and mutated closures and all that nonsense: write the code to be straightforward and obviously correct.
Your code should work, but that's an unnecessarily complicated way to do it.
Try
this.Aggregate((min, next) => next < min ? next : min);
精彩评论