I ran into this little if statement while fixing moving some code around today. It does something like 18 string functions and that is assuming that Path.GetExtension is just one string function call.
30 if (Path.GetExtension(path).ToLower() == ".tif"
31 || Path.GetExtension(path).ToLower() == ".pdf"
32 || Path.GetExtension(path).ToLower() == ".jpg"
33 || Path.GetExtension(path).ToLower() == ".png"
34 || Path.GetExtension(path).ToLower() == ".gif"
35 || Path.GetExtension(path).ToLower() == ".jpeg")
I decided that it needed to be removed and I replaced it with this.
25 string ext = Path.GetExtension(path).ToLower();
26 List<string> validExt = new List<string> { ".tif", ".pdf", ".jpg", ".png", ".gif", ".jpeg" };
27 if(validExt.Contains(ext))
But you could also do this with LINQ
25 var extensions = new List<string> { ".jpg", ".jpeg", ".tif", ".png", ".gif" };
26 if ((from i in extensions where i == Path.GetExtension(dir).ToLower() select i).Count() > 0)
I like the second version the best but that is just personal preference. Both alternatives are way better then the first option.
No comments:
Post a Comment