There are some cool and exciting features in .NET 3.5/C# 3.0, and with those features comes some darn interesting ways to write the exact same line of code.
Using the above stated tool set (and by extension .NET 2.0 stuff), what are the different ways the below code snippet could reasonably be rewritten?
string uploadDirectory = 'c:\\some\\path\\'; if (Directory.Exists(uploadDirectory)) { string[] files = Directory.GetFiles(uploadDirectory); foreach (string filename in files) { if (File.GetLastWriteTime(filename).AddHours(12) < DateTime.Now) { File.Delete(filename); } } }
Lambda:
Edit: On 2nd thought, you can avoid the security lookups on each File access by using DirectoryInfo and FileInfo instead of the static File methods:
And for those missing their own Each method:
To make this really crazy, and fit the C# 3.0 theme, let’s throw in an anonymous type:
But that just doesn’t make any sense. 😉