我应该让我的私人类方法静态的? [英] Should I make my private class methods static?

查看:123
本文介绍了我应该让我的私人类方法静态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有作出私有方法的类的静态最佳实践?我有一类的一些方法。其中的一些可以很容易地制成静态的,因为它们简单地处理数据。

Is there a best practice for making private methods in classes static? I have a class with a few methods. Some of these can easily be made static, since they simply process data.

我应该让他们静态或刚刚离开他们的是?这更多的是风格的问题?是否有性能方面的考虑?

Should I make them static or just leave them as is? Is this more of a style issue? Are there performance considerations?

编辑: <一href="http://stackoverflow.com/questions/169378/c-method-can-be-made-static-but-should-it">http://stackoverflow.com/questions/169378/c-method-can-be-made-static-but-should-it

推荐答案

如果该方法不访问任何类型的国家,他们应该是静态的。

If the methods don't access any of the type's state then they should be static.

中的静态方法调用提供了实例方法提高性能和静态方法的presence告诉你的code未来的读者,调用此方法将创建在当前实例的状态没有副作用的类型。

Static method calls provide a performance gain over instance methods and the presence of a static method tells future readers of your code that calling this method will create no side effects in the the state of the current instance of the type.

静态方法的性能提升来自于一个事实,即编译器没有发出 callvirt 指令调用静态方法。该 callvirt 指令是方便的实例调用,它做了检查调用方法之前。但是,当你调用一个静态方法没有必要检查所以编译器就可以发射更快通话指令,不检查

The performance gain of a static method comes from the fact that the compiler does not have to emit callvirt instructions to call a static method. The callvirt instruction is handy for instance calls in that it does a null check prior to calling the method. However, when you call a static methods there is no need to check for null so the compiler is free to emit the faster call instruction which doesn't check for null.

这篇关于我应该让我的私人类方法静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆