0
When should a static method be used?
I never understood why using the static method and when to use it, could anyone explain it to me?
5 Réponses
+ 1
so you can use it anywhere...
u use static variables so that you can access and update them with every object.
+ 1
"These are called with the type name. No instance is required—this makes them slightly faster. Static methods can be public or private."
The main thing is that you don't need to have an instance of an object to call your static method.
Source: https://www.dotnetperls.com/static
😉
+ 1
A non-static method has its owner as an additional parameter.
tiger.eat(kitten)
Is the same as
eating(tiger,kitten)
But something there is no real owner. Or at most exactly one owner, like when you want to talk to the system clipboard. There is exactly one of those. So in C# standard library, the Clipboard class has only static methods.
+ 1
Static methods are good candidates for writing Utility functions as utilities aren't intended to maintain any state and creating objects for utilities may sound overkill. Static methods are one way to manage Singletons too. Some other use cases are to write some initialization code on app startup. Hope it helps!
0
Thanks guys 👊