Initialize value inside function only once [duplicate]
Possible Duplicate:
Static variables in C#
If you have a large function and in the middle somewhere you have a value that should be declared only the first time its encounter.
In c++ you can use static开发者_开发知识库 for this:
void func() {
...
...
static double startPosition = 0.0;
int var = startPositino - value;
startPosition = var;
...
}
But in c# you cant have static variables inside a function, is there some other way to do this without declaring it outside the function?
bool changed = true;
void func() // the large function from the question (it wasn't specified what it does or what is called)
{
.....
if(changed)
{
// here you initalize you variable (the static from the c++)
changed = false;
}
.....
}
精彩评论