为什么ReSharper的建议常量,静态操作? [英] Why does resharper suggest const, static operations?

查看:151
本文介绍了为什么ReSharper的建议常量,静态操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,为什么ReSharper的建议的方法是静态非静态类? 它是用于保存创建实例?它是性能问题? 还有,为什么它建议'常量'一些PARAMS?它是性能问题? 我很想得到一些解释

I was wondering why resharper suggests a method to be static in non static class? Is it for saving the creation of the instance? Is it a matter of performance? also, why does it suggest to 'const' some params? Is it a matter of performance ? I would love to get some explanation

推荐答案

在编译器遇到它发出通话指令的静态方法,但它遇到一个实例方法时,它会发出 callvirt 指令。现在 callvirt 指令检查对象是否是在进行调用之前空。所以连接到它的性能损失。但它有助于使方法调用polymorphycally。

When compiler encounters a static method it emits call instructions but when it encounters an instance method it emits callvirt instruction. Now the callvirt instruction checks the object whether it is null before making the call. so there is a performance penalty attached to it .But it helps in making the method call polymorphycally.

因此​​,如果方法不带班是advisiable作出这样的方法静态的任何财产状态的变化有关,因为它提高了效果进行

so if the method is not associated with a change of state of any property of the class it is advisiable to make that method static as it improves the peformance

关于使用const它的价值,而不是在运行时编译时间关联。所以常量的所有变量得到由在编译时本身,这明显提高了性能的数值取代。

Regarding the use of const it is a compile time association of the value rather than at runtime. so all variables of the const get replaced by the value at compile time itself which obviously improves the performance.

这篇关于为什么ReSharper的建议常量,静态操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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