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

前端 未结 11 970
闹比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:07

    You cannot use static methods to implement an interface, and you cannot override static methods. So using static methods means that you are simply not doing OOP.

    Think about how you would implement the following functionality using only static methods?

    interface IDocument 
    {
       void Print(IDevice targetDevice);
    }
    
    IDocument instance;
    
    instance = new PdfDocument();
    instance.Print(printer);
    
    instance = new WordDocument();
    instance.Print(printer);
    

提交回复
热议问题