C# static vs instance methods

后端 未结 8 731
再見小時候
再見小時候 2020-12-05 18:10

I\'m risking it this might be a newb question but here goes. I\'m tempted to add a method to a class that could possible have thousands and thousands of instances in memory

8条回答
  •  广开言路
    2020-12-05 18:37

    You don't need a method "Rename" at all; you already have one, it's the setter of the Name property. Instance methods do not require more and more memory on a per instance basis, that would be silly.

    Don't make design decisions like this before profiling your code. Adding an instance method to a class is not going to cause a performance bottleneck, and even if it did, you shouldn't start out with a bad design based on a guess; prove it is a bottleneck and make adjustments as necessary.

提交回复
热议问题