每次运行方法时增加+1

编程入门 行业动态 更新时间:2024-10-25 00:23:56
本文介绍了每次运行方法时增加+1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

} 我想增加开始+1它每次只显示1个方法运行 i想要 RunFirstTime显示1个计数 RunSecondTime显示2个计数 等等..

解决方案

以下是您的提示:查看定义 _i 的上下文及其生命周期。 尝试理解一件事:本地(堆栈)变量仅在通话期间存在。任何局部变量将一次又一次初始化为相同的值。除局部变量外,还有哪些其他对象?它们的生命周期是什么? 顺便说一下,不要使用这些名称,尝试遵循(良好)Microsoft命名约定,并且永远不要使用太短的名称(和尽量避免太长时间。)
对于非常高级的理解,与您使用指定方法的确切情况无关:我写的内容局部变量并非在所有100%的情况下都是如此。排除项附带闭包: en.wikipedia/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: en.wikipedia/wiki/Closure_%28computer_programming%29[^].
—SA

更多推荐

每次运行方法时增加+1

本文发布于:2023-10-22 22:26:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518911.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!