我最近写了一些代码,我无意中将一个变量名重用作为一个动作的参数,该动作在一个已经具有同名变量的函数中声明。例如:
var x = 1;
Action<int> myAction = (x) => { Console.WriteLine(x); };
Run Code Online (Sandbox Code Playgroud)
当我发现重复时,我惊讶地发现代码编译并完美运行,根据我对 C# 中作用域的了解,这不是我所期望的行为。一些快速的谷歌搜索出现了抱怨类似代码确实会产生错误的SO 问题,例如Lambda Scope Clarification。(我将该示例代码粘贴到我的 IDE 中以查看它是否可以运行,只是为了确保它可以完美运行。)此外,当我在 Visual Studio 中进入“重命名”对话框时,第一个x
被突出显示为名称冲突。
为什么这段代码有效?我在 Visual Studio 2019 中使用 C# 8。
我在Visual Studio 2008中收到以下错误:
Error 1 A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else
这是我的代码:
for (int i = 0; i < 3; i++)
{
string str = "";
}
int i = 0; // scope error
string str = ""; // no scope error
Run Code Online (Sandbox Code Playgroud)
我知道str
一旦循环终止就不再存在,但我也认为范围i
也局限于for
循环.
那么i
与在for
循环之外声明的变量具有相同的范围吗? …
请考虑以下代码示例:
// line #
{ // 1
// 2
{ // 3
double test = 0; // 4
} // 5
// 6
double test = 0; // 7
} // 8
Run Code Online (Sandbox Code Playgroud)
这给出了错误
名为'test'的局部变量不能在此范围内声明,因为它会给'test'赋予不同的含义,'test'已在'child'范围内用于表示其他内容
但我不明白为什么?外部测试变量从第7行开始,而不是在第2行中,所以在第4行声明第二个变量名为test的问题,其中范围在第5行结束?
如何根据简单条件更改双变量中的项目?
检查这个例子:
public partial class Form1 : Form
{
double[] vk = new double[11] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
...
...
void setDouble()
{
if (bunifuDropdown1.selectedIndex == 0)
{
double[] vk = new double[11] { 2, 4.86, 11.81, 28.68, 69.64, 169.13, 410.75, 997.55, 2422.61, 5883.49, 21000 };
}
if (bunifuDropdown1.selectedIndex == 1)
{
double[] vk = new double[11] { 2, 4.51, 10.14, 22.81, 51.31, 115.46, 259.78, 584.51, 1315.14, 2959.07, 21000 };
} …
Run Code Online (Sandbox Code Playgroud) 可能重复:
与c#中的范围混淆
看来,在C#的变量与局部范围的if/else /循环块与定义之外的块下面的变量冲突的定义 - 见代码剪断.在C/C++和Java下,等效代码编译得很好.这是C#中的预期行为吗?
public void f(){
if (true) {
/* local if scope */
int a = 1;
System.Console.WriteLine(a);
} else {
/* does not conflict with local from the same if/else */
int a = 2;
System.Console.WriteLine(a);
}
if (true) {
/* does not conflict with local from the different if */
int a = 3;
System.Console.WriteLine(a);
}
/* doing this:
* int a = 5;
* results in: Error 1 A local variable named 'a' …
Run Code Online (Sandbox Code Playgroud) 下面的代码在Visual Studio 2012中生成此错误; "名为'x'的局部变量不能在此范围内声明,因为它会给'x'赋予不同的含义,'x'已在'子'范围内用于表示其他内容"
for (int x = 0; x < 9; x++)
{
//something
}
int x = 0;
Run Code Online (Sandbox Code Playgroud)
然而.如下修改我得到这个错误; "当前上下文中不存在名称'x'"
for (int x = 0; x < 9; x++)
{
//something
}
x = 0;
Run Code Online (Sandbox Code Playgroud)
这是一个巨大的矛盾还是我错过了什么?
我很好奇在for-loops(etc)的初始化部分中声明的变量范围背后的设计考虑因素.这些变量似乎既不在范围内,也不在范围之内,或者我错过了什么?为什么这个以及何时需要?即:
for (int i = 0; i < 10; i++)
{
}
i = 12; //CS0103: The name 'i' does not exist in the current context
int i = 13; //CS0136: A local variable named 'i' cannot be declared in this scope
//because it would give a different meaning to 'i', which is already
//used in a 'child' scope to denote something else
Run Code Online (Sandbox Code Playgroud) 不确定这是否是因为 C# 编译器非常挑剔,但我尝试在 C# 中执行此操作:
public static void Main()
{
bool result = true; // some dummy value
if(result)
{
int x = 5;
Console.WriteLine(x);
}
int x = 10;
Console.WriteLine(x);
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨变量名“x”已被使用:
不能在此作用域中声明名为“x”的局部变量,因为它会给“x”赋予不同的含义,而“x”已在“子”作用域中用于表示其他内容。
我知道它认为这是一个范围问题,但为什么它会这么认为呢?
如果我用 Java 重现相同的代码,就没有问题。