微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在一个或多个for循环中使用现有的variables?

我正在通过Head First C#工作,而且我对目前的练习有些困惑。 他们指出:

如果你在一个for循环中声明一个variables – for(int c = 0; …) – 那么这个variables只在循环的大括号内有效。 所以如果你有两个使用variables的for循环, 你可以在每个循环中声明它,或者在循环外部声明一个声明。 如果variablesc已经被声明在循环之外,你不能在任何一个中使用它。

这听起来有点矛盾,就好像说你只能在外面使用它,但如果你在外面声明,你就不能使用它。

那么你能,还是不能? 我试图在两个单独的for循环中声明c,并且它工作正常,但是当在for循环之外声明c时,我无法find任何方法来引用循环中的variablesc,同时也在外部声明,无论我是否尝试改变它的价值与否。 这不是练习所需要的,我只是想把我遇到的每一点知识都吸收过来,试图超越材料。

从C#中正在增长的文件中读取?

什么版本的Windows将支持.NET 4.0?

.Net越来越多的内存问题

如何检查应用程序的另一个实例是否正在运行

有没有办法改变整个机器的GC行为?

这本书可能会令我困惑,所以如果这是不可能的,完全没有必要,请告诉我,谢谢!

如何检测Windows 10在Windows窗体应用程序中进入平板电脑模式的时间?

如何在OS X和Linux上安装MSBuild?

通过本地networking的URL调用子程序?

log4net xmlconfigurator.configure需要使用dllembedded的xml文件

SymFromAddr使用C#

这个问题是范围界定的问题之一。 在这里阅读一些关于如何在C#中使用变量范围的细节。

如果一个变量是在一个循环之外声明的,你不能在里面重新声明它:

坏的

int c = 0; for(int c = 0; c < list.Count; c++) // Error! { }

OK

在外面宣布,用在里面 :

int c = 0; for(c = 0; c < list1.Count; c++) { } for(c = 0; c < list2.Count; c++) { }

在两个循环内声明:

for(int c = 0; c < list1.Count; c++) { } for(int c = 0; c < list2.Count; c++) { }

你可以做

int i; for (i = 0; i < 3; i++) foo(i); for (i = 0; i < 5; i++) bar(i);

要么

for (int i = 0; i < 3; i++) foo(i); for (int i = 0; i < 5; i++) bar(i);

但不是

int i; for (int i = 0; i < 3; i++) //error foo(i); for (int i = 0; i < 5; i++) bar(i);

如果我的理解正确的话,这个陈述确实是令人困惑的,根据文本,我不应该这样做:

int i; for (i = 1; i < 10; i++) { } for (i = 0; i < 20; i++) { }

但我可以,这显然是有效的。 我想文本的意思是“你不能在任何一个重新声明 ”,而不是“你不能在任何一个中使用”。

我认为他们是指以下内容

你可以这样做。 听到的是你已经在循环之外声明了一个变量并正在使用它。 但问题是你可能会覆盖你需要在别的地方使用的现有值。

int i = 0; for (i = 0; i < 100; i++) { // Do something }

你真的不能做的是这个。 在这里,你正在重新使用外部的变量在内部for 。

for (int i = 0; i < 100; i++) { for (i = 0; i < 100; i++) { // Do something } }

这里的概念是Scope 。 变量在某个范围内声明,不能在其外部访问。 这有助于定义变量的生命周期以及控制对其的访问。 变量可以在方法内的类,方法或条件范围内声明,例如在if语句或for循环中。

一个简单的思考范围的方法是你可以访问一个变量

在一对花括号{ ... }它生活在下面。

这是一个例子:

public class TestClass { int p; // p's is in the 'TestClass' scope public void TestFunction1() { Console.WriteLine(p); // OK,p in class scope // a lives in the 'TestFunction' scope int a = 1; // Declared outside of any loop. for (int i = 0; i < 10; i++) { // i lives in the scope of this for loop Console.WriteLine(i); // a is still accessible since this for loop is inside TestFunction1 Console.WriteLine(a); } Console.WriteLine(a); // OK,in scope //Console.WriteLine(i); // Error,i out of scope // j also lives in the TestFunction1 scope int j = 0; for (j = 0; j < 20; j++) { // j still accessible within the loop since the loop is within TestFunction1 Console.WriteLine(j); } Console.WriteLine(j); // Ok,j still in scope (outside of loop) } public void TestFunction2() { Console.WriteLine(p); // Ok,TestFunction2 is in the TestClass scope like TestFunction1 //Console.WriteLine(a); // Error,a lives in TestFunction1 //Console.WriteLine(i); // Error,allright Now we're just getting silly ; ) } }

您可以使用现有变量作为for循环的起点。

我刚刚开始学习C#4周前,所以要疲倦..但语法是:

int x = 10; for (x = x ; x < 15; x++) // x starts off as whatever defined above (15) { Console.WriteLine("x is: {0}",x); } // After for is done executing,x will = 15

那么不论出于什么原因,你可以继续做其他的逻辑(当X <30时,发生其他事情)ex)

for (x = x ; x < 30; x++) { // Do stuff here }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐