在 C# 中使用私有静态方法而不是私有方法
什么时候建议使用私有静态方法而不是私有[实例]方法?
编辑: 我正在寻找一个好的(或最佳实践)。我想知道微软是否使用了这项技术?有人知道吗?我找不到任何博客、文章或示例源代码来解释这个主题。
任何帮助将不胜感激。
When its recommended to use a private static method instead of a private [instance] method?
EDIT:
i am looking for a good (or best practice) about that .i am wondering Is this technic used by microsoft or not?does anybody know something about that ? i coudnt find any blog ,article or sample source code that explains this topic.
any help would be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
方法声明开头的
Static
字样,基本上是无状态的标志,所以里面发生的事情是一个纯粹的动作,或者至少应该是。如果你想使用
private static
:像你的类的API函数一样使用它,只做一些计算/报告......而不改变对象的实际状态,这基本上是通过实例方法完成的。这是一种预期的实现方式,并不意味着强制,但由于它是预期,它将帮助其他开发人员理解您的代码,并帮助您在几年后理解代码,那时您将回到您的项目并已经忘记了一切。
问候。
Static
word in the beginning of the method declaration, basically is a sign of stateless, so what is happening inside is a pure action, or at least should be.If you want to use
private static
: use it like API functions of your class that just make some calculations/reports... and not change the actual state of the object, which basically is done byinstance
methods.This is an expected way of implementing, which doens't mean that is mandatory, but as it's expected, it will help other developers understand your code, and help understand code to you after a couple of years, when you will come back to your project and already have forgot everything.
Regards.