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.
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.
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.
Member
1 Points
1 Post
Difference between class method and static method, when to use which?
Mar 31, 2017 09:51 AM|abbas_ali_91|LINK
What is the main difference between class method and class static method, does it have impact on performance when to use it?
All-Star
31362 Points
7055 Posts
Re: Difference between class method and static method, when to use which?
Mar 31, 2017 10:39 AM|kaushalparik27|LINK
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:
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
All-Star
15186 Points
3888 Posts
Re: Difference between class method and static method, when to use which?
Mar 31, 2017 11:21 AM|raju dasa|LINK
Hi,
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.
rajudasa.blogspot.com || rajudasa-tech
All-Star
48530 Points
18075 Posts
Re: Difference between class method and static method, when to use which?
Mar 31, 2017 11:28 AM|PatriceSc|LINK
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.
Contributor
2096 Points
1040 Posts
Re: Difference between class method and static method, when to use which?
Apr 01, 2017 06:40 PM|Khuram.Shahzad|LINK
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.