Sam Harwell
Sam Harwell

Reputation: 99949

Should `System.IO.Path` be concrete?

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:

Opinions?

Upvotes: 2

Views: 229

Answers (1)

Chris Chilvers
Chris Chilvers

Reputation: 6479

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.

Upvotes: 2

Related Questions