new FileInfo(path).Name versus Path.GetFileName(path)

后端 未结 3 550
醉梦人生
醉梦人生 2020-12-09 19:28

which one is better to use and why? I mean in which aspects these two commands differ and how? Performance, readability, ...

new FileInfo(path).Name or

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-09 20:03

    Performancewise Path.GetFilename() will outperform the other version as it is static. Your first version creates an object which has to be instantiated and garbage collected.

    Readability: Path.GetFilename() clearly wins IMHO!

    The way they figure out the name won't differ much I think.

提交回复
热议问题