locked
Difference between class method and static method, when to use which? RRS feed

  • Question

  • User-2019599894 posted

    What is the main difference between class method and class static method, does it have impact on performance when to use it?

    Friday, March 31, 2017 9:51 AM

Answers

  • User-1780421697 posted

    Difference between static and non-static class method:

    The only difference between a static and a non-static method is that a non-static method has a hidden "this" parameter passed to it that allows you to access all the non-static fields in the object. static method does not get passed the hidden "this" parameter, and therefore does not have access to any of the object's non-static fields

    When to use?

    static methods are used when you will need to access a method from many different classes or forms, you wouldn't want to create an object every time you needed that method. and you certainly wouldn't want to retype or copy and paste the same method  into every class you needed it in.

    Performance?

    Yes when you created only one method to use then you do have, Like you have a method of Utility class which Capitalize the words or some thing like that then you can have a static method.

    Static method are not able to access non-static property of the class, it depends on parameters, like in method above ,which perform some operation on string parameter you just really concern about string parameter.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 1, 2017 6:40 PM

All replies

  • User-1509636757 posted

    In theory, a static method should perform slightly better than an instance method, all other things being equal, because of the extra hidden this parameter. In practice, this makes so little difference that it'll be hidden in the noise of various compiler decisions. (Hence two people could "prove" one better than the other with disagreeing results). Not least since the this is normally passed in a register and is often in that register to begin with. a static call is 4 to 5 times faster than constructing an instance every time you call an instance method. However, we're still only talking about tens of nanoseconds per call, so you're unlikely to notice any benefit unless you have really tight loops calling a method millions of times, and you could get the same benefit by constructing a single instance outside that loop and reusing it.

    From below referenced link:

    1. c# - Performance of static methods vs instance methods
    2. oop - Performance of using static methods vs instantiating the class containing the methods
    Friday, March 31, 2017 10:39 AM
  • User527778624 posted

    Hi,

    What is the main difference between class method and class static method, does it have impact on performance when to use it?

    AFAIK, In multi threading scenario, each thread can create instance of class and use class method without any block.

    If static method is used by multiple threads, then you need to come up with locking (block) otherwise you get corrupt results or errors.

    Friday, March 31, 2017 11:21 AM
  • User753101303 posted

    Hi,

    At best it would be a micro optimization (ie no visible effect unless maybe in very rare intensive calculation cases). Just use what make sense from a design point of view. See https://msdn.microsoft.com/en-us/library/79b3xss3.aspx for details about static classes and members in C#.

    In short if a method doesn't have to use a particular object instance (of its own class), it can be static.

    For example https://msdn.microsoft.com/en-us/library/system.math(v=vs.110).aspx is a static class (and so it can only have static methods) as it will just compute something using exclusively the parameters provided by each call.

    Friday, March 31, 2017 11:28 AM
  • User-1780421697 posted

    Difference between static and non-static class method:

    The only difference between a static and a non-static method is that a non-static method has a hidden "this" parameter passed to it that allows you to access all the non-static fields in the object. static method does not get passed the hidden "this" parameter, and therefore does not have access to any of the object's non-static fields

    When to use?

    static methods are used when you will need to access a method from many different classes or forms, you wouldn't want to create an object every time you needed that method. and you certainly wouldn't want to retype or copy and paste the same method  into every class you needed it in.

    Performance?

    Yes when you created only one method to use then you do have, Like you have a method of Utility class which Capitalize the words or some thing like that then you can have a static method.

    Static method are not able to access non-static property of the class, it depends on parameters, like in method above ,which perform some operation on string parameter you just really concern about string parameter.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 1, 2017 6:40 PM