Breaking loop when “warnings()” appear in R

You can turn warnings into errors with: options(warn=2) Unlike warnings, errors will interrupt the loop. Nicely, R will also report to you that these particular errors were converted from warnings. j <- function() { for (i in 1:3) { cat(i, “\n”) as.numeric(c(“1”, “NA”)) }} # warn = 0 (default) — warnings as warnings! j() # … Read more

Check for null in foreach loop

Just as a slight cosmetic addition to Rune’s suggestion, you could create your own extension method: public static IEnumerable<T> OrEmptyIfNull<T>(this IEnumerable<T> source) { return source ?? Enumerable.Empty<T>(); } Then you can write: foreach (var header in file.Headers.OrEmptyIfNull()) { } Change the name according to taste 🙂