Why doesn't C# support static method variables?

Why doesn't C# support static method variables?

Rate This
  • Comments 19

Q: In C++, it's possible to write a static method variable, and have a variable that can only be accessed from inside the method. C# doesn't provide this feature. Why?

A: There are two reasons C# doesn't have this feature.

First, it is possible to get nearly the same effect by having a class-level static, and adding method statics would require increased complexity.

Second, method level statics are somewhat notorious for causing problems when code is called repeatedly or from multiple threads, and since the definitions are in the methods, it's harder to find the definitions.

[Author: Eric Gunnerson]

Leave a Comment
  • Please add 1 and 5 and type the answer here:
  • Post

  • Thank you for your explanations, but I'm not at all happy with it...

    First, there are a lot of C# features which could be done another way, but we like C# because it offers already known features more elegant.
    Second, threading as such is also 'somewhat notorious for causing problems' but that does not stop us using it. Additionally, it is not at all elegant to have class-level static variables which in fact belongs to a method. They get degraded to 'class global variables' and it's just a question of time until another developers misuse them and we will earn side effects.
    Of course, it's possible to put such a method in it's own class, but I think we don't really have to discuss such 'solutions' here.

    I'm sure, that static method variabls must be used carefully, but then, they make sense and are very useful.

    kind regards,
    Thomas
    Schittli
  • I must agree with Thomas, especially when the C# team decided to add features such as partial types - don't you those are going to make finding definitions of anything a lot harder than method static variables? I hope those three reasons posted are not the actual reasons the C# design team based their decisions on.

    As for method static variables being problematic when the method is called from multiple threads - how exactly are they different from class static members? From the implementation point of view they're class static members that are scoped to a single method.
  • I'm with the C# team on this one. A method itself doesn't logically have state outside the period during which it's being called. If you've got a method which has separate state, it should be a separate object - or the state should be part of the class.
  • I agree, I don't believe that a method should encapsulate data. That is the purpose of an object.
  • ironically vb.net supports them. Go figure.
  • It seems to me there are two angles of approach. The language designers are trying to follow a rationale that fits the principles underlying the language. Programmers are trying to do a job and know which features they find useful. The C# team have had a rare opportunity to design a language from scratch and are keen to keep it as ‘clean’ as possible. On the other hand the VB team are re-designing an existing language and, I suspect, are more likely to keep features which are arguably not ideal but serve a very useful purpose. The idea of a variable which is scoped to a method but retains its value between calls is very old and useful in certain circumstances. Enumeration is one which comes to mind. On balance, rather than write a whole class to do a single simple job, I would prefer to be able to do it in a single function.
  • "From the implementation point of view they're class static members that are scoped to a single method" -- Jerry

    In C++ static function variables are instantiated the first time the function is called. The assignment can also only be called once. But I'm not sure how, either by having the assignment function static, inline, or both. (Should be written in More Effective C++ by Scott Meyers).

  • I absolutely agree with Jerry. Besides VB has them, so it's not due to GC difficulties or anything else. It also promotes encapsulation

  • C# does allow state scoped to a method in other contexts, autoproperties and events. I'd rather allow it with a warning, then the coders can set policies to control whether they want to allow it to compile or not.

  • in C# every thing is in class. It cannot have method like in c , c++ which does not belong to any class.Hence there is no point in using static class method variables.

    At any time we have an alternate to declare static member variable for that method

    i.e

    class A

    {

    public void fun1()

    {

    static int count =0; //oops its an error in c#

    }

    public void fun2()

    {

    static int count =0; //oops its an error in c#

    }

    }

    For  this  we can always have alternate like

    class A

    {

    static int fun1_count =0;

    static int fun2_count =0;

    public void fun1()

    {

    }

    public void fun2()

    {

    }

    }

  • yes, we can use a class level variable (private),

    but a method-level static is really nice for encapsulating things that are used for recursion implementation

  • you can use

    public int GetTen()

    {

      const int ten = 10;

     return ten;

    }

    with that you have the "STATIC METHOD VARIABLE" if for some reazon you need to have that ugly line of code.

  • Those aren't reasons, they are ridiculous excuses. All declarations (including 'using') should be allowed everywhere, to allow for localization of reference ... uses of the same name (or other information) should be grouped as close together as possible.

  • " A method itself doesn't logically have state outside the period during which it's being called."

    Utter nonsense.

  • How about this for a good use. I would really like this to not be a global variable since i have lots of registry values to read.

    class a{

    shared bool some_value;

    public static bool hasEverBeenConfigured

    {

    get

    {

      static bool readRegistry = true;

      if(readRegistry)

      {

          readRegistry = false;

          some_value = readfromregistry;

      }

      return some_value;

    }

    }

    }

Page 1 of 2 (19 items) 12