每次运行方法时增加+1 [英] increase +1 everytime the methods run

查看:103
本文介绍了每次运行方法时增加+1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  int  _i =  0 ; 
private void 增加()
{
_i ++;



}



我想增加开始+1它每次只显示1个方法运行

i想要

RunFirstTime显示1个计数

RunSecondTime显示2个计数

等等..

解决方案

以下是您的提示:查看定义 _i 的上下文及其生命周期。

尝试理解一件事:本地(堆栈)变量仅在通话期间存在。任何局部变量将一次又一次初始化为相同的值。除局部变量外,还有哪些其他对象?它们的生命周期是什么?



顺便说一下,不要使用这些名称,尝试遵循(良好)Microsoft命名约定,并且永远不要使用太短的名称(和尽量避免太长时间。)



对于非常高级的理解,与您使用指定方法的确切情况无关:我写的内容局部变量并非在所有100%的情况下都是如此。排除项附带闭包 http://en.wikipedia.org/wiki/Closure_ %28computer_programming%29 [ ^ ]
-SA

int _i = 0;
private void Increase()
{
    _i++;


}

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).

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: http://en.wikipedia.org/wiki/Closure_%28computer_programming%29[^].
—SA


这篇关于每次运行方法时增加+1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆