Advantages to Using Private Static Methods

后端 未结 8 2193
南笙
南笙 2020-11-27 09:52

When creating a class that has internal private methods, usually to reduce code duplication, that don\'t require the use of any instance fields, are there performance or mem

相关标签:
8条回答
  • 2020-11-27 10:08

    From the FxCop rule page on this:

    After you mark the methods as static, the compiler will emit non-virtual call sites to these members. Emitting non-virtual call sites will prevent a check at runtime for each call that ensures that the current object pointer is non-null. This can result in a measurable performance gain for performance-sensitive code. In some cases, the failure to access the current object instance represents a correctness issue.

    0 讨论(0)
  • 2020-11-27 10:12

    It'll be slightly quicker as there is no this parameter passed (although the performance cost of calling the method is probably considerably more than this saving).

    I'd say the best reason I can think of for private static methods is that it means you can't accidentally change the object (as there's no this pointer).

    0 讨论(0)
  • 2020-11-27 10:13

    Yes, the compiler does not need to pass the implicit this pointer to static methods. Even if you don't use it in your instance method, it is still being passed.

    0 讨论(0)
  • 2020-11-27 10:13

    This forces you to remember to also declare any class-scoped members the function uses as static as well, which should save the memory of creating those items for each instance.

    0 讨论(0)
  • 2020-11-27 10:14

    When I'm writing a class, most methods fall into two categories:

    • Methods that use/change the current instance's state.
    • Helper methods that don't use/change the current object's state, but help me compute values I need elsewhere.

    Static methods are useful, because just by looking at its signature, you know that the calling it doesn't use or modify the current instance's state.

    Take this example:

    public class Library
    {
        private static Book findBook(List<Book> books, string title)
        {
            // code goes here
        }
    }

    If an instance of library's state ever gets screwed up, and I'm trying to figure out why, I can rule out findBook as the culprit, just from its signature.

    I try to communicate as much as I can with a method or function's signature, and this is an excellent way to do that.

    0 讨论(0)
  • 2020-11-27 10:24

    As has already been stated, there are many advantages to static methods. However; keep in mind that they will live on the heap for the life of the application. I recently spent a day tracking down a memory leak in a Windows Service... the leak was caused by private static methods inside a class that implemented IDisposable and was consistently called from a using statement. Each time this class was created, memory was reserved on the heap for the static methods within the class, unfortunately, when the class was disposed of, the memory for the static methods was not released. This caused the memory footprint of this service to consume the available memory of the server within a couple of days with predictable results.

    0 讨论(0)
提交回复
热议问题