How would you re-write the following, so it’s a bit easier on the eyes?
Func<DateTime, String> formatter = null;
formatter = new Func<DateTime, String>(d =>
{
var r = "";
foreach (var i in new[] { d.Day, d.Month, d.Year })
{
if (i < 10) r += "0";
r += i.ToString();
}
return r;
});
I’d rewrite it as:
Only difference here is that for dates before 1000AD, this will pad to 4 digits instead of 2…
EDIT: As noted in comments, this doesn’t replicate the bug in the original code – it would format (say) a month of 5 as “055”. The simplest way to fix this in the original code would be:
(But I’d prefer my code anyway 🙂