开发者

Initialize value inside function only once [duplicate]

This question already has answers here: Closed 11 years ago.

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;
   }

   .....
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜