LINQ: Not Any vs All Don’t

Implementation of All according to ILSpy (as in I actually went and looked, rather than the “well, that method works a bit like …” I might do if we were discussing the theory rather than the impact). public static bool All<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) { if (source == null) { throw Error.ArgumentNull(“source”); } … Read more

Access to Modified Closure (2)

Prior to C# 5, you need to re-declare a variable inside the foreach – otherwise it is shared, and all your handlers will use the last string: foreach (string list in lists) { string tmp = list; Button btn = new Button(); btn.Click += new EventHandler(delegate { MessageBox.Show(tmp); }); } Significantly, note that from C# … Read more

What does CultureInfo.InvariantCulture mean?

Not all cultures use the same format for dates and decimal / currency values. This will matter for you when you are converting input values (read) that are stored as strings to DateTime, float, double or decimal. It will also matter if you try to format the aforementioned data types to strings (write) for display … Read more

Invert “if” statement to reduce nesting

It is not only aesthetic, but it also reduces the maximum nesting level inside the method. This is generally regarded as a plus because it makes methods easier to understand (and indeed, many static analysis tools provide a measure of this as one of the indicators of code quality). On the other hand, it also … Read more