Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the advantage of Path.TryJoin over Path.Combine in .NET Core?

Tags:

c#

path

.net-core

I just started using .NET Core 2.1, and found the Path.TryJoin and Path.Join method. There is no documentation on the method.

I ran some unit tests calling the method, and it did nothing different than Path.Combine.

Is there any advantage to this other than utilizing the new C# Span<T> data type to minimize string manipulation execution?

like image 864
krillgar Avatar asked Sep 24 '18 18:09

krillgar


People also ask

Why we use path combine?

Path. Combine uses the Path. PathSeparator and it checks whether the first path has already a separator at the end so it will not duplicate the separators. Additionally, it checks whether the path elements to combine have invalid chars.

What is System IO path?

A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk; for example, a path might map to a location in memory or on a device. The exact format of a path is determined by the current platform.

What is a path in C#?

C# path class comes under System.IO namespace and System. Runtime. dll assembly. This class is used to perform operations on string instances that have file path or directory path information. A path is a string that holds the location of the file or directory and it can be an absolute or relative location.


1 Answers

You can find the rationale behind the Path.Join being introduced here. IMHO it seems to be trading simplicity for performance and some minor fixes, also couldn't find any indication that Join creates cross-platform valid paths with the correct separators like Path.Combine does but by all accounts is should.

To elaborate on the discussion, the main quirk that Path.Join resolves is the last rooted argument which a lot of people don't expect to work the way it does (which seamed minor but clearly is not), as can be seen in the examples here on the the worst gotcha in C# or .NET page.

The general idea from the discussion is to slowly deprecate Combine in favor of Join.

Also note string overloads have been added for the Path.Join with this PR

like image 168
MarkovskI Avatar answered Sep 30 '22 05:09

MarkovskI