none
Static RRS feed

  • Question

  • I'm about to get back into programming in C# and have some confusion about the STATIC keyword. In my previous incarnation as a C# developer I was told by some very good programmers, biased as they were, that one should never use static for variables, methods, properties, just never ever use it. I didn't fully understood, just did my best to comply with the team. So tonight I'm dusting off some code with the KnapSack algorithm  in it. I recall this was a very useful tool for figuring out the lowest costs for some items to be included in pricing quotes. Won't bore you with details but as I said this tool works very good and my testing tonight with it proves it will most likely work for this upcoming project. But it contains the static keyword in the method in the class and thus I was wondering if it's absolutely necessary and if so were these previous team members giving me not so accurate information?

    public class Knapsack
        {
            public static int KnapSack(int capacity, int[] weight, int[] value, int itemsCount)
            { 
                int[,] K = new int[itemsCount + 1, capacity + 1];
    
                for (int i = 0; i <= itemsCount; ++i)
                {
                    for (int w = 0; w <= capacity; ++w)
                    {
                        if (i == 0 || w == 0)
                            K[i, w] = 0;
                        else if (weight[i - 1] <= w)
                            K[i, w] = Math.Max(value[i - 1] + K[i - 1, w - weight[i - 1]], K[i - 1, w]);
                        else
                            K[i, w] = K[i - 1, w];
                    }
                }
                return K[itemsCount, capacity];
            }
        }

    Sunday, August 4, 2019 10:32 PM

Answers

  • Greetings Bill.

    Your question is a little broad, but the short answer is that your former colleagues were wrong. There are definitely situations where static is useful.

    Think of the Math class. All the methods - Cos, Sin, Min, Max, and so on - are static, as are constants like PI.

    • Marked as answer by Bill Tillman Tuesday, August 6, 2019 2:38 PM
    Sunday, August 4, 2019 11:25 PM
  • > one should never use static for variables, methods, properties, just never ever use it.

    You never say "never" in programming.  "static" is a perfectly useful concept.  Is it possible this people were talking about C++?  Static is more useful in C# than it is in C+ because everything has to be in a class, but even in C++ is still has its place.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Bill Tillman Tuesday, August 6, 2019 2:38 PM
    Monday, August 5, 2019 5:23 AM
  • Hi Bill,

    Thank you for posting here.

    I think you and your colleagues have some misunderstandings about static. MSDN has a detailed document, you could have a look.

    Static Classes and Static Class Members (C# Programming Guide)

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Bill Tillman Tuesday, August 6, 2019 2:38 PM
    Monday, August 5, 2019 6:05 AM
    Moderator
  • Greetings again Bill.

    I've reread your post, and I'm thinking you might not understand the implications of static in the Knapsack example.

    If the method is static, you can call it (as I'm sure you know) in one line, like so.

    int knapsackResult = Knapsack.KnapSack(/* arguments go here */);

    But without making it static, you would have to instantiate an object of the class first ('new' it).

    Knapsack knapsack = new Knapsack();
    int knapsackResult = knapsack.KnapSack(/* etc. */);

    It seems fairly obvious in this case that instantiating would be an irritating waste of time, similar to the way you shouldn't have to instantiate Math to get the sine of an angle.


    • Edited by Ante Meridian Monday, August 5, 2019 6:33 AM Missed a couple of capital 'S's.
    • Marked as answer by Bill Tillman Tuesday, August 6, 2019 2:38 PM
    Monday, August 5, 2019 6:30 AM
  • Hello,

    idea of using static method (or class) went from object paradigm. It says object has state which is given by fields and behavior which is defined by methods. Methods in this case are gateway to provide and change object state. It is about encapsulation principle which says nobody can change object state directly by changing field.

    Properties in C# are get/set methods which encapsulate access into field. 

    If state can be shared across multiple instances of same class fields can be static. Method which change this field can be static so. But it doesn't make sense in many situation. In future object responsibility can be changed and state sharing across multiple instances can be problem. It is better don't use static there. 

    Better method how to share object state is something from domain model. It is called Value Object. This instance can be shared from some Value Object pool. 

    Math class has static method because it doesn't hold any inner state. So it is right place to use static. 

    • Marked as answer by Bill Tillman Tuesday, August 6, 2019 2:38 PM
    Monday, August 5, 2019 9:33 AM

All replies

  • Greetings Bill.

    Your question is a little broad, but the short answer is that your former colleagues were wrong. There are definitely situations where static is useful.

    Think of the Math class. All the methods - Cos, Sin, Min, Max, and so on - are static, as are constants like PI.

    • Marked as answer by Bill Tillman Tuesday, August 6, 2019 2:38 PM
    Sunday, August 4, 2019 11:25 PM
  • > one should never use static for variables, methods, properties, just never ever use it.

    You never say "never" in programming.  "static" is a perfectly useful concept.  Is it possible this people were talking about C++?  Static is more useful in C# than it is in C+ because everything has to be in a class, but even in C++ is still has its place.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    • Marked as answer by Bill Tillman Tuesday, August 6, 2019 2:38 PM
    Monday, August 5, 2019 5:23 AM
  • Hi Bill,

    Thank you for posting here.

    I think you and your colleagues have some misunderstandings about static. MSDN has a detailed document, you could have a look.

    Static Classes and Static Class Members (C# Programming Guide)

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Bill Tillman Tuesday, August 6, 2019 2:38 PM
    Monday, August 5, 2019 6:05 AM
    Moderator
  • Greetings again Bill.

    I've reread your post, and I'm thinking you might not understand the implications of static in the Knapsack example.

    If the method is static, you can call it (as I'm sure you know) in one line, like so.

    int knapsackResult = Knapsack.KnapSack(/* arguments go here */);

    But without making it static, you would have to instantiate an object of the class first ('new' it).

    Knapsack knapsack = new Knapsack();
    int knapsackResult = knapsack.KnapSack(/* etc. */);

    It seems fairly obvious in this case that instantiating would be an irritating waste of time, similar to the way you shouldn't have to instantiate Math to get the sine of an angle.


    • Edited by Ante Meridian Monday, August 5, 2019 6:33 AM Missed a couple of capital 'S's.
    • Marked as answer by Bill Tillman Tuesday, August 6, 2019 2:38 PM
    Monday, August 5, 2019 6:30 AM
  • Hello,

    idea of using static method (or class) went from object paradigm. It says object has state which is given by fields and behavior which is defined by methods. Methods in this case are gateway to provide and change object state. It is about encapsulation principle which says nobody can change object state directly by changing field.

    Properties in C# are get/set methods which encapsulate access into field. 

    If state can be shared across multiple instances of same class fields can be static. Method which change this field can be static so. But it doesn't make sense in many situation. In future object responsibility can be changed and state sharing across multiple instances can be problem. It is better don't use static there. 

    Better method how to share object state is something from domain model. It is called Value Object. This instance can be shared from some Value Object pool. 

    Math class has static method because it doesn't hold any inner state. So it is right place to use static. 

    • Marked as answer by Bill Tillman Tuesday, August 6, 2019 2:38 PM
    Monday, August 5, 2019 9:33 AM