- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我已经使用 C# 很长时间了,但从未意识到以下几点:
public static void Main()
{
for (int i = 0; i < 5; i++)
{
}
int i = 4; //cannot declare as 'i' is declared in child scope
int A = i; //cannot assign as 'i' does not exist in this context
}
如果它不允许我用这个名字声明一个变量,那么为什么我不能在 for block 之外使用 'i' 的值呢?
我认为 for 循环使用的迭代器变量仅在其范围内有效。
最佳答案
不允许在 for 循环中和 for 循环外定义同名变量的原因是外部作用域中的变量在内部作用域中有效。这意味着如果允许的话,for 循环中将有两个“i”变量。
参见:MSDN Scopes
具体来说:
The scope of a local variable declared in a local-variable-declaration (Section 8.5.1) is the block in which the declaration occurs.
和
The scope of a local variable declared in a for-initializer of a for statement (Section 8.8.3) is the for-initializer, the for-condition, the for-iterator, and the contained statement of the for statement.
还有:Local variable declarations (C# 规范第 8.5.1 节)
具体来说:
The scope of a local variable declared in a local-variable-declaration is the block in which the declaration occurs. It is an error to refer to a local variable in a textual position that precedes the local-variable-declarator of the local variable. Within the scope of a local variable, it is a compile-time error to declare another local variable or constant with the same name.
(强调我的。)
这意味着您的 for 循环中 i
的范围是 for 循环。而在 for 循环之外的 i
范围是整个 main 方法加上 for 循环。这意味着您将在循环中出现两次 i
,根据上述内容,这是无效的。
不允许您执行 int A = i;
的原因是因为 int i
仅适用于 for
循环。因此它不再可以在 for
循环之外访问。
如您所见,这两个问题都是范围界定的结果;第一个问题 (int i = 4;
) 将在 for
循环范围内产生两个 i
变量。而 int A = i;
会导致访问超出范围的变量。
您可以做的是将 i
声明为整个方法的作用域,然后在方法和 for 循环作用域中使用它。这将避免违反任何一条规则。
public static void Main()
{
int i;
for (i = 0; i < 5; i++)
{
}
// 'i' is only declared in the method scope now,
// no longer in the child scope -> valid.
i = 4;
// 'i' is declared in the method's scope -> valid.
int A = i;
}
编辑:
当然可以更改 C# 编译器以允许此代码非常有效地编译。毕竟这是有效的:
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
for (int i = 5; i > 0; i--)
{
Console.WriteLine(i);
}
但是,如果能够编写如下代码,对您的代码可读性和可维护性真的有好处吗:
public static void Main()
{
int i = 4;
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
for (int i = 5; i > 0; i--)
{
Console.WriteLine(i);
}
Console.WriteLine(i);
}
想想这里可能会出错,最后的i
打印出来的是0还是4?现在这是一个非常小的示例,一个非常容易理解和跟踪的示例,但与用不同的名称声明外部 i
相比,它的可维护性和可读性肯定要差得多。
注意:
请注意,C# 的作用域规则不同于 C++'s scoping rules .在 C++ 中,变量仅在从声明它们的位置到 block 末尾的范围内。这将使您的代码成为 C++ 中的有效构造。
关于c# - for循环中声明的变量是局部变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7992332/
我在覆盖 ReSwift Pod 中的函数时遇到问题。我有以下模拟类(class): import Foundation import Quick import Nimble import RxSwi
我有一个类似于下面的继承结构。我正在采用 Printable 协议(protocol)并努力覆盖 description 属性。我遇到了一个谷歌此时似乎不知道的奇怪错误,提示为第三类,并引用了第二类和
我有一个类“Cat”和 Cat 类的一个子类“DerivedCat”。 Cat 有一个函数 meow(),而 DerivedCat 覆盖了这个函数。 在应用程序中,我声明了一个 Cat 对象: Cat
Kotlin 变量 变量是用于存储数据值的容器。 要创建一个变量,使用 var 或 val,然后使用等号(=)给它赋值: 语法 var 变量名 = 值 val 变量名 = 值 示例 va
C 中的所有标识符在使用前都需要声明,但我找不到它在 C99 标准中表示的位置。 我觉得也是指宏定义,不过定义的只是宏展开顺序。 最佳答案 C99:TC3 6.5.1 §2,脚注 79 明确指出: T
今天我的博客提要显示错误: This page contains the following errors: error on line 2 at column 6: XML declaration
在编写 IIF 语句、表和下面给出的语句时出现错误。 陈述: SELECT IIF(EMP_ID=1,'True','False') from Employee; table : CREATE TAB
我正在创建一个登录 Activity ,我希望它在按下登录按钮时显示进度对话框,我声明、初始化并调用了它,但它没有显示。但是当我在创建时调用进度对话框时,它出现了 这是我的代码: public cla
当我输入声明语句时: Vector distance_vector = new Vector(); 我收到错误(在两种情况下都在“双”下划线): Syntax error on token "doub
我正在本地部署在docker-for-desktop中。这样我将来可以迁移到kubernetes集群。 但是我面临一个问题。使用永久卷时,docker容器/ pod中的目录将被覆盖。 我正在拉最新的S
我有一个 MyObject 类型的对象 obj,我声明了它的实例。 MyObject obj; 但是,我没有初始化它。 MyObject 的类看起来像: public class MyObject {
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 9 年前。 Improv
这个问题已经有答案了: Android: Issue during Arraylist declaration (1 个回答) 已关闭 9 年前。 有时我会看到 ArrayList 声明如下 Arra
我对java比较陌生,经过大量搜索,我无法将相关问题的任何解决方案与我的解决方案配对。我正在尝试实现一种非常简单的方法来写入/读取数组,但编译器无法识别它。 “键盘”也是一个“无法识别的变量”。这是数
简短:何时分配内存 - 在声明或初始化时? 长整型:int x;将占用与int z = 10;相同的内存。 此外,这对于包含更多数据的自定义对象将如何工作。假设我有这个对象: public class
我需要使用此程序更好地理解函数定义、声明和正确调用。我真的需要了解如何使用它们。您能否向我展示编写此程序的正确方法(所有三个都正确并进行解释)? #include #include quad_eq
这是我的主要功能以及我要传递的内容。 int main(void){ struct can elC[7]; // Create an array of stucts Initiali
我想知道是否有更好的方法来完成此任务; 我有一个对象 - 其中一个属性是字典。我有一组逗号分隔值。我需要过滤 Dictionary 并仅获取 Dictionary 值至少与其中一个值匹配的那些元素 这
下面的using-declarations有什么意义 using eoPop::size; using eoPop::operator[]; using eoPop::back; using eoPo
我的问题更像是一个关于 for 循环样式的好奇问题。在阅读别人的一些旧代码时,我遇到了一种我以前从未见过的风格。 var declaredEarlier = Array for(var i=0, le
我是一名优秀的程序员,十分优秀!