How to decide between C# static and non-static methods?

前端 未结 11 975
闹比i
闹比i 2020-12-24 03:35

[Edit]

My original-question was \"Why to decide between static and non-static? Both do the same...\"

Unfortunately it was edited to a C#-specific question wh

11条回答
  •  别那么骄傲
    2020-12-24 04:20

    If you have to ask, don't use statics.

    Actual rule of thumb (and there are plenty of real technical reasons but I find this helps explain the concepts):

    • If the class in question can exist multiple times it's not static.

    • If the method in question acts against the instance information it's not static.

    • If the method or class is about meta-information it's static.

    With those guidelines it's clear that files and documents are multiples and copying is an act against the instance. The method should not be static.

提交回复
热议问题