Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most useful .NET utility classes developers tend to reinvent rather than reuse [closed]

Tags:

.net

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.

like image 493
Chris Burgess Avatar asked Oct 07 '08 13:10

Chris Burgess


People also ask

Are utility classes bad practice?

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.

What is utility class in C#?

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 Util class?

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.


1 Answers

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()

like image 196
Vivek Avatar answered Oct 04 '22 14:10

Vivek