|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Advantages of Static Vs Instance methods?Hello All,
Can anyone please list me the advantages or disadvantages of instance and static methods in terms of performance be it either memory consumption or CPU utilization or any other parameters. Thank you. Wow, interesting question... if kind of odd.
Performance wise two identical methods implemented as static and instance would have very (very) little difference in terms of performance. Certainly nothing to worry about! The choice of static vs instance methods is typically more about design. FxCop views the matter fairly simplistically - if your method doesn't touch any members of the class then it should be static. As a rule of thumb though, one isn't any faster then the other. Josh http://www.thejoyofcode.com/ Josh,
You said that instance and static have very little difference in terms of performance. I am curious to know what that difference is and why? Thank you. Show quote "Josh Twist" wrote: > Wow, interesting question... if kind of odd. > > Performance wise two identical methods implemented as static and > instance would have very (very) little difference in terms of > performance. Certainly nothing to worry about! > > The choice of static vs instance methods is typically more about > design. FxCop views the matter fairly simplistically - if your method > doesn't touch any members of the class then it should be static. > > As a rule of thumb though, one isn't any faster then the other. > > Josh > http://www.thejoyofcode.com/ > > A static method can be called without an instance of the class being
created. e.g. public class Example { public static void Method1() { // this is a static method } public void Method2() { // this is an instance method } } Method1 is a static method of the Example class and is called like this: Example.Method1() Method2 is an instance method of the Example class and is called on an instance of an Example class: Example myInstance = new Example(); myInstance(); At the end of the day they're both methods and have no real difference in terms of performance. Which one to use is a question of design. I suggest you have a bit of a read up on OOP (Object Oriented Programming) to help you make these decisions Josh http://www.thejoyofcode.com/ |
|||||||||||||||||||||||