- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果类方法不依赖于类的状态,那么它可以是静态的。在这种情况下,将此类方法设为静态是好的还是坏的做法?
(标记为 C#,但可能适用于方法必须是类成员的许多 OO 语言。)
最佳答案
Visual Studio Code Analysis 和 ReSharper 建议将这些方法设为静态,因为这会带来微小的性能优势:
来自 documentation :
Members that do not access instance data or call instance methods can be marked as static (Shared in Visual Basic). After you mark the methods as static, the compiler will emit nonvirtual call sites to these members. Emitting nonvirtual call sites will prevent a check at runtime for each call that makes sure that the current object pointer is non-null. This can achieve a measurable performance gain for performance-sensitive code. In some cases, the failure to access the current object instance represents a correctness issue.
对于非“性能敏感”代码,这是一个品味问题。如果我没有充分的理由不这样做,我个人会遵守 ReSharper 的建议。
关于c# - 使无状态方法静态化是好事还是坏事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41488027/
我最近一直在阅读有关 SPA 以及它有多好的内容。 我想知道是否有人可以告诉我什么时候应该使用,或者什么时候不应该像常规 MVC 一样使用 SPA 最佳答案 MVC(Rails、MVC4)的工具更加成
当涉及到标签上的非标准属性时,HTML(或者可能只是 XHTML?)相对严格。如果它们不是规范的一部分,那么您的代码将被视为不合规。 但是,非标准属性对于将元数据传递给 Javascript 可能非常
我正在定义我的 DbConntextObj _container.RegisterType(new HttpContextLifetimeManager()); Unity 没有在 lifetimem
我们看到大量虚拟内存碎片和内存不足错误,然后达到了 3GB 限制。 编译调试在 web.config 中设置为 true,但我从每个人那里得到了不同的答案,调试设置为 true 是否会导致每个 asp
我是一名优秀的程序员,十分优秀!