+ 5
Why we are using static keyword before all methods? Is it necessary? What is the use and importance?
5 Respuestas
+ 2
by using static infornt of a method u can call that method directly by the class name, without declaring the instance of the class
+ 1
Static methods are conceptually the same as static variables, thus the reasons to use or not use them are similar. They belong to the class, not specific objects of that class. An example from the java API is Math, all the variables are static.
0
I think of static methods as Class methods, not Object methods. They are useful when you do not need an instance of an object for logic to complete, or you can pass an object to work with as a parameter. Class itself may have static fields and properties, for example to track count or pool of instances of an object.
For example Convert.ToInt32 is a static function, you can use any string object as a parameter. It would be awkward for String object to have all possible conversion functions as non static methods. Even more, some non static methods simply call static version of that method, so all real logic is contained in static one.
0
in these examples they use static to enable methods to be called within Main, because you cannot call non static within static method.
- 2
if you use static keyword you dont have to create new object before using it.