Well, I canβt finally say which is better, because they serve for different purposes.
Are you familiar with OOP? In OOP, static objects or members of a class that can be accessed directly from the class, while non-static members can only be accessed from the instance to which it belongs.
C # follows a similar principle for methods. Static methods can be accessed directly from the class, while non-static methods (or instance methods, as I call them) must be accessible from the instance. This is why instatiating needs to be done, for example, with methods, while for static methods this is simply not necessary, much less impractical (see below).
In OOP, static variables are used for values ββthat cannot be stored by an instance variable. Example: suggested that you want to keep the number of instances of the class? How can you save this in one copy?
The methods use a similar principle. They should be used for procedures for which it is not practical to do this in an instance of the class. I tend to use them for broad procedures (rather than technical terms), which means those that do not require me to instantiate an object. Example: adding two parameters. (This use may or may not be correct, but I believe it is)
However, if you want to add two properties of an object, the method cannot be static, because, as you soon realized, static methods cannot access methods or instance variables inside the class. Of course, this makes sense, because this static method would not know which instance of the class would receive them, unless that was said, since it is not part of the instance itself)
In order not to complicate the situation, I will stay here. Let me know if you understand something.
Arash Afshinfar Dec 14 '12 at 11:43 2012-12-14 11:43
source share