2

Members of the Path class like Path.Combine are indispensable. They lead to great, clean, correct code. Unfortunately, if you use it for something like managing header paths in a C++ preprocessor, you'll quickly realize that it's showing up more than expected in the profiler. Why?

Exceptions: ArgumentException: path1 or path2 contain one or more of the invalid characters defined in GetInvalidPathChars.

Even without the check, the combine command is O(n) (since it has to copy the string contents). However, that is much less expensive than checking for the existence of any of GetInvalidPathChars' ~40 members.

I believe that the Path class should be an immutable type that contains a string known to not contain any invalid characters. Static members should be provided for operating on string (as exists now) and an identical set added for working on Path objects. Making this change in the .NET Framework:

  • Is not a breaking change (changing a static class to a sealed concrete type is non-breaking)
  • Significantly improves performance of several best-practices operations

Opinions?

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280

1 Answers1

2

The Uri class can provide that sort of functionality if you don't mind the file:// syntax. I think .Net prefers the use of the Uri class anyway since that can allow locating of resources other than just local files, although this does require extra work on the program side to support that.

Chris Chilvers
  • 6,429
  • 3
  • 32
  • 53