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?