3

I came to know that,

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.

That is, local variables are not allowed as static inside a method.

Therefore, the below code will not compile

public class Base1
{    
    public int getHighscoreString()
    {
        int highscore = Int32.MinValue;

        static int max = 10; // It is not allowed here.

        if(max>highscore)
            highscore = max;

        return highscore;

    }
}

But, we can always do the same functionality by

public class Base1
    {    
        static int max = 10; 
        public int getHighscoreString()
        {
            int highscore = Int32.MinValue;

            if(max>highscore)
                highscore = max;

            return highscore;

        }
    }

So, is it a design decision that static variable can not be used as local variable inside a method or any reason behind it?

Noor A Shuvo
  • 2,639
  • 3
  • 23
  • 48

1 Answers1

-1

A static variable is a variable that lives beyond the instances of the class in which is declared, in the sense that to get or set its value you have to call its class type and not its instances. This happens because a reference to the memory address where the static variable lives is kept regardless of the construction or disposal of instances of that class. Say you have a Zoo class:

public class Zoo
{
    public static int counter;
    public string Name;

    public void AddAnimal(string name)
    {
        Name = name;
        counter++;
    }
}

The counter increases every time you add an animal to the zoo, and you can get the total anytime by calling

Zoo myZoo = new Zoo()
myZoo.AddAnimal(“tiger”);
Zoo.counter; // 1
myZoo.counter // wrong!

Apart from myZoo.counter being wrong, the main thing to notice is that even if we dispose of the myZoo instance, the counter field will always keep the total of the animals in the zoo!

On the contrary, variables declared in a method are immediately disposed after method execution and therefore you cannot reference them outside of that method because the memory address where the variable “lived” is not available anymore.

I hope this clarifies the matter a little more. Cheers!

Davide Vitali
  • 1,017
  • 8
  • 24
  • Yeah, of course, this clarifies the matter. Can you please explain, as other languages (VB, C++ ) support it, what is the point there? – Noor A Shuvo Mar 05 '19 at 03:03
  • 2
    This doesn't answer the OP's question at all. It's like if someone asked if you could explain why anti-vaxxers don't get immunized and your response is "vaccines prevent illness". – Enigmativity Mar 05 '19 at 04:18
  • @Enigmativity he asked what’s the reason behind c# not supporting static in local variables (last sentence of his question), why should my answer be out of scope (pun intended)? – Davide Vitali Mar 05 '19 at 05:18
  • @DavideVitali - I don't see anywhere in your answer where you give any design reasons. You just seem to be stating how C# works. Just a side note: "variables declared in a method are immediately disposed after method execution" isn't true. – Enigmativity Mar 05 '19 at 05:30