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

依然范特西╮ 提交于 2019-12-23 09:26:27

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/52485588/what-is-the-advantage-of-path-tryjoin-over-path-combine-in-net-core

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!