本文介绍了每次运行方法时增加+1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于非常高级的理解,与您使用指定方法的确切情况无关:我写的内容局部变量并非在所有100%的情况下都是如此。排除项附带闭包: en.wikipedia/wiki/Closure_ %28computer_programming%29 [ ^ ] -SA int _i = 0;
private void Increase()
{
_i++;
For very advanced understanding, not related to your exact case with the named method: what I wrote about local variables is so not in all 100% cases. The exclusions come with closures: en.wikipedia/wiki/Closure_%28computer_programming%29[^]. —SA
int _i = 0 ; private void 增加() { _i ++;
} 我想增加开始+1它每次只显示1个方法运行 i想要 RunFirstTime显示1个计数 RunSecondTime显示2个计数 等等..
解决方案以下是您的提示:查看定义 _i 的上下文及其生命周期。 尝试理解一件事:本地(堆栈)变量仅在通话期间存在。任何局部变量将一次又一次初始化为相同的值。除局部变量外,还有哪些其他对象?它们的生命周期是什么? 顺便说一下,不要使用这些名称,尝试遵循(良好)Microsoft命名约定,并且永远不要使用太短的名称(和尽量避免太长时间。)
} I want to increase start +1 it only displays 1 everytime the methods run i want RunFirstTime display 1 count RunSecondTime display 2 count and so on..
解决方案 Here is the hint for you: look at the context where _i is defined and its lifetime. Try to understand one thing: local (stack) variables live only during the call. Any local variable will be initialized to same value again and again. What are other objects, except local variables? What are their lifetimes? By the way, don't use such names, try to follow (good) Microsoft naming conventions, and never use too short names (and try to avoid too long ones).更多推荐
每次运行方法时增加+1
发布评论