What is the difference between Environment.CurrentDirectory and Directory.GetCurrentDirectory?

前端 未结 3 2036
刺人心
刺人心 2021-02-11 17:00

In .NET what is the difference between:

  • Environment.CurrentDirectory
  • Directory.GetCurrentDirectory()?

Of course

相关标签:
3条回答
  • 2021-02-11 17:36

    No, there are no other differences.

    0 讨论(0)
  • 2021-02-11 17:37

    As David says: they do the same thing. Internally, when getting Environment.CurrentDirectory it will call Directory.GetCurrentDirectory and when setting Environment.CurrentDirectory it will call Directory.SetCurrentDirectory.

    Just pick a favorite and go with it.

    0 讨论(0)
  • 2021-02-11 17:37

    As per other answers, there is no difference - the implemenetation of Environment.CurrentDirectory delegates to the Get and Set methods in Directory.

    There's an interesting stylistic API-design question that that raises - why did the designers of Environment feel that a regular property was appropriate, whereas the designers of Directory preferred explicit Get and Set methods?

    The Framework Design Guidelines book has a fair amount to say about choosing properties versus methods, some of which is available online. The most relevant parts seem to me to be (with my emphases):

    A rule of thumb is that methods should represent actions and properties should represent data. Properties are preferred over methods if everything else is equal

    ...

    • CONSIDER using a property, if the member represents a logical attribute of the type

    ...

    • DO use a method, rather than a property, in the following situations:
      • The operation is orders of magnitude slower than a field access would be

    All things considered my opinion is that explicit Get and Set methods better represent what is going on here.

    0 讨论(0)
提交回复
热议问题