Most useful .NET class developers tend to invent rather than reuse

I recently read this post by Phil Haack ( The Most Useful .NET Classes, Class Developers Tend to Reuse, Not Reuse ) from last year, and thought I would see if anyone has any additions to the list.

+55
Oct 07 '08 at 13:49
source share
25 answers

People tend to use the following, which is ugly and associated with failure:

string path = basePath + "\\" + fileName; 

Better and safer:

 string path = Path.Combine(basePath, fileName); 

I also saw how people write their own method for reading all bytes from a file. It is very convenient:

 byte[] fileData = File.ReadAllBytes(path); // use path from Path.Combine 

As TheXenocide noted, the same goes for File.ReadAllText() and File.ReadAllLines()

+46
Oct 07 '08 at 14:26
source share

String.IsNullOrEmpty()

+36
Oct 07 '08 at 13:54
source share
 Path.GetFileNameWithoutExtension(string path) 

Returns the file name of the specified path string without extension.

 Path.GetTempFileName() 

Creates the same name, zero byte temporary file on disk and returns the full path to this file.

+35
Oct 07 '08 at 14:05
source share

Class System.Diagnostics.Stopwatch .

+28
Oct 07 '08 at 14:46
source share

String.Format.

How many times have i seen

 return "£" & iSomeValue 

but not

 return String.Format ("{0:c}", iSomeValue) 

or people adding percent signs are such things.

+23
Oct 7 '08 at 13:53
source share

Enum.Parse ()

+22
Oct 07 '08 at 13:53
source share

Trying to find out where My documents are located on the user's computer. Just use the following:

 string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
+20
07 Oct '08 at 14:42
source share

I needed to download some files recently in a windows application. I found the DownloadFile method for the WebClient object:

  WebClient wc = new WebClient(); wc.DownloadFile(sourceURLAddress, destFileName); 
+20
Oct 07 '08 at 19:51
source share

String.Join () (however, almost everyone knows about string.Split and seems to use it every chance they get ...)

+19
Oct 07 '08 at 13:55
source share

Hard coding a / to directory manipulation string compared to using:

 IO.Path.DirectorySeparatorChar 
+14
Oct 07 '08 at 14:15
source share

The StringBuilder class and especially the AppendFormat method.

PS: If you are looking for a measure of the performance of string operations: StringBuilder vs. String / Fast String Operations with .NET 2.0

+13
Oct 07 '08 at 14:32
source share
 Environment.NewLine 
+12
Oct 07 '08 at 16:19
source share

Instead of generating the file name with Guid, just use:

 Path.GetRandomFileName() 
+12
Oct 07 '08 at 21:33
source share

Many of the new Linq features seem rather unknown:

Any<T>() & All<T>()

 if( myCollection.Any( x => x.IsSomething ) ) //... bool allValid = myCollection.All( x => x.IsValid ); 

ToList<T>(), ToArray<T>(), ToDictionary<T>()

 var newDict = myCollection.ToDictionary( x => x.Name, x => x.Value ); 

First<T>(), FirstOrDefault<T>()

 return dbAccessor.GetFromTable( id ). FirstOrDefault(); 

Where<T>()

 //instead of foreach( Type item in myCollection ) if( item.IsValid ) //do stuff //you can also do foreach( var item in myCollection.Where( x => x.IsValid ) ) //do stuff //note only a simple sample - the logic could be a lot more complex 

All very useful features that can be used outside of Linq syntax.

+10
Jan 06 '09 at 17:07
source share
+9
Oct 08 '08 at 5:06
source share

System.Text.RegularExpressions.Regex

+8
Oct 07 '08 at 13:55
source share

input.StartsWith("stuff") instead of Regex.IsMatch(input, @"^stuff")

+8
Oct 07 '08 at 2:00
source share

See Hidden .NET Base Class Library Classes

+8
Oct 07 '08 at 14:42
source share

For everything that lurks in the Microsoft.VisualBasic namespace, TextFieldParser is actually a very nice csv parser. I see that many people either roll back their own (badly) or use something like a beautiful Fast CSV library on Code Plex, without even knowing that they are already baked within the framework.

+7
Jun 17 '10 at 22:52
source share

File.

 using System.IO; File.Exists(FileNamePath) Directory.Exists(strDirPath) File.Move(currentLocation, newLocation); File.Delete(fileToDelete); Directory.CreateDirectory(directory) System.IO.FileStream file = System.IO.File.Create(fullFilePath); 
+6
Oct 07 '08 at 14:18
source share

System.IO.File.ReadAllText versus writing logic using StreamReader for small files.

System.IO.File.WriteAllText versus writing logic using StreamWriter for small files.

+6
Oct 07 '08 at 14:25
source share

It seems like many people like going through an XML file manually to find something, rather than using XPathNaviagator.

+4
Oct 07 '08 at 13:57
source share

Most people forget that Directory.CreateDirectory () gracefully degrades if the folder already exists, and wrap it with a meaningless call to if (! Directory.Exists (....)).

+4
Oct 07 '08 at 16:00
source share

myString.Equals (anotherString)

and options, including culture-specific.

I am sure that at least 50% of the developers write something like: if (s == "id") {...}

+1
Jan 6 '09 at 16:19
source share

Path.Append is always forgotten in what I saw.

-5
Oct 07 '08 at 2:30 p.m.
source share



All Articles