oop - c# - static method or not, which one is the best? -


i know there posts on didn't find answer i'm looking for, , simple.

i know calling method static or not (instanciation (oop)).

example, using static:

using system;  namespace projet_test {     class surfacescalcul     {         public static int calc_air(int a, int b){             int result = * b;              return result;         }     }      /*      * ##########################      * ##########################      */      class mainclass     {         public static void main (string[] args)         {             console.writeline(surfacescalcul.calc_air(4,5));         }     } } 

you call class(surfacescalcul).method , can access method.

--

normal, using oop:

using system;  namespace projet_test {     class surfacescalcul     {         public int calc_air(int a, int b){             int result = * b;              return result;         }     }      /*      * ##########################      * ##########################      */      class mainclass     {         public static void main (string[] args)         {             surfacescalcul calculmethis = new surfacescalcul();              console.writeline(calculmethis.calc_air(4,5));         }     } } 

you create instantiation , can access methods in class.

--

[my question] 1 best, i'm wondering why using static method more instance or inverse. there real reason or both good. there best time use 1 more other?

i'll argue both sides , can choose.

consider first example static function:

namespace projet_test {     class surfacescalcul     {         public static int calc_air(int a, int b){             int result = * b;              return result;         }     }      /*      * ##########################      * ##########################      */      class mainclass     {         public static void main (string[] args)         {             console.writeline(surfacescalcul.calc_air(4,5));         }     } } 

this fine if never care overriding or kind of inheritance.

now consider example:

class surfacescalcul {     public virtual int calc_air(int a, int b){         int result = * b;          return result;     } } class surfacescalcul2 : surfacescalcul {    pubic override int calc_air(int a, int b)    {       var g = base.calc_air(a, b);       return g*2;     } }  /*  * ##########################  * ##########################  */  class mainclass {     public static void main (string[] args)     {         var calculmethis = new surfacescalcul2();          console.writeline(calculmethis.calc_air(4,5));     } } 

you're not changing "correctness" of program @ allowing polymorphism thing.

this example trivial matter. i'd prefer route class since static methods can't have instance variables , can't kind of dependency injection, makes unit testing easier. object instantiation allows different instances contain different state information static can't contain instance specific data.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -