JavaScript ?? vs ||: Keep Zero When Zero Is a Valid Value
A fallback value can quietly change valid input. If zero is a meaningful value, using logical OR may not do what you…
Defining a LINQ query does not always mean its results have already been calculated. Some operations wait until the sequence is enumerated. Understanding the timing helps explain results that seem to change unexpectedly.
var numbers = new List<int> { 1, 2 };
var query = numbers.Where(n => n > 1);
numbers.Add(3);
Console.WriteLine(string.Join(", ", query));
For this LINQ-to-Objects example, the output is 2, 3. The Where query is enumerated after the list changes. Microsoft describes deferred execution as delaying evaluation until the value is needed. This example does not imply that every LINQ operator or provider behaves identically.
Change the query assignment to numbers.Where(n => n > 1).ToList() before adding 3. The resulting list contains 2. You have materialised those results at that point. That is different from promising a deep copy of every object a query might return.
Repeated enumeration may repeat work, so understand the source before choosing how to consume it. Do not add ToList everywhere without considering memory and intended behaviour. In a Malta .NET interview, walk through the order of statements and predict the result. A tiny example often reveals more understanding than a broad claim that LINQ is always lazy.
Browse current technology vacancies with Huwell Consultancy and ask us about the responsibilities that matter to you.
Explore opportunitiesA fallback value can quietly change valid input. If zero is a meaningful value, using logical OR may not do what you…
A C# object that implements IDisposable may own a resource that needs a clear end of use. A using statement helps express…
BigDecimal can represent decimal values precisely, but the value you pass into it still matters. A number already represented as a double…