I recently read this Phil Haack post (The Most Useful .NET Utility Classes Developers Tend To Reinvent Rather Than Reuse) from last year, and thought I'd see if anyone has any additions to the list.
As already mentioned, a class using Utility Class is responsible not only for its original role, but also for obtaining its dependencies. Another problem is that existing Utility Classes have tendencies to rot. Usually, such classes are created from a code, which has no better or proper place to be.
Utility Class (. NET Micro Framework) provides a collection of helper functions you can use to configure settings for security, collections, driver manipulation, time, and idle CPU usage.
What is a Utils class? A Utils class is a general purposed utility class using which we can reuse the existing block of code without creating instance of the class.
People tend to use the following which is ugly and bound to fail:
string path = basePath + "\\" + fileName;
Better and safer way:
string path = Path.Combine(basePath, fileName);
Also I've seen people writing custom method to read all bytes from file. This one comes quite handy:
byte[] fileData = File.ReadAllBytes(path); // use path from Path.Combine
As TheXenocide pointed out, same applies for File.ReadAllText()
and File.ReadAllLines()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With