c#: static variable in a static method

41,868

Solution 1

No.

You can't have local static variables.

From MSDN:

C# does not support static local variables (variables that are declared in method scope).

And here:

The static modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, destructors, or types other than classes.

As you can see, local variables are not mentioned.


You can, however use a static field:

public class MyClass
{
    private static int MyVariable = 10;

    public static void MyMethod()
    {
        MyVariable++;
    }
}

Solution 2

No, but you could have:

private static int x = 0;
public static void MyMethod()
{
     x++;
} 
Share:
41,868

Related videos on Youtube

Craig Johnston
Author by

Craig Johnston

Updated on January 16, 2022

Comments

  • Craig Johnston
    Craig Johnston over 2 years

    Can you have a static variable in a static method? Would the value of this variable be preserved across all calls to the method?

    eg.

    public static void MyMethod()
    {
        static int x = 0;
        x++;
    }
    
    • David Heffernan
      David Heffernan over 13 years
      What you need is a static class member
  • AustinWBryan
    AustinWBryan about 6 years
    Unfortunate because if MyVariable is only used in MyMethod it belongs in MyMethod, not exposed to the rest of the class.
  • Joel Trauger
    Joel Trauger about 6 years
    This is probably the single disadvantage of using C# in favor of an older "C" language. C++ scoping was weaker, but you could have local static variables. This is what make the strtok function even possible. From the POSIX strtok page: This function uses static storage to keep track of the current string position between calls. I wrote a variant of it in C++ that ignored empty records and accepted any delimiter that could be represented by a C++ String class. Knowing how these things work should make it replicate-able in any language, but alas, this is one weakness of C#.
  • Clearer
    Clearer almost 6 years
    @JoelTrauger How exactly does C++ have weaker scoping rules than C#? As far as I can tell, they are identical, except that C++ may have local static variables.
  • Joel Trauger
    Joel Trauger over 5 years
    IIRC, in C++ you have explicitly invoke inheritance to gain the properties of a class. Anything not explicitly passed forward through inheritance is automatically private. In C#, just make the class public and you automatically inherit all the public members of that class. C# also allows for internal scoping to further control the scoping of a class member. Because of that I consider C++ scoping to be weaker, though others might find it stronger...since static variables do not exist in C#.
  • Chris F Carroll
    Chris F Carroll over 2 years
    I think you will mostly want [ThreadStatic]private static int x = 0; if you do that?
  • matt
    matt over 2 years
    the issue is that it is likely in this kind of thing that you don't want x accessed directly by other parts of the class. I'm sure the answer would be "well just wrap everything in a static class then" But that seems like a lot of boilerplate.