A local variable named 'port' cannot be declared

This question already has an answer here: C# variable scoping: 'x' cannot be declared in this scope because it would give a different meaning to 'x' 3 answers You have a variable named port in your code already. So it doesn't allow you to declare it again in your foreach loop. Change it's name to something else: foreach (string port2 in ports) { comboBox1.Items.

名为'port'的局部变量不能声明

这个问题在这里已经有了答案: C#变量范围:'x'不能在这个范围内声明,因为它会给'x'3个答案以不同的含义 您的代码中已经有一个名为port的变量。 所以它不允许你在你的foreach循环中再次声明它。 将其名称更改为其他名称: foreach (string port2 in ports) { comboBox1.Items.Add(port2); }

C# variable scoping: 'x' cannot be declared in this scope because it would give a different meaning to 'x'

if(true) { string var = "VAR"; } string var = "New VAR!"; This will result in: Error 1 A local variable named 'var' cannot be declared in this scope because it would give a different meaning to 'var', which is already used in a 'child' scope to denote something else. Nothing earth shattering really, but isn't this just plain wrong? A fellow developer and I we

C#变量范围:'x'不能在这个范围内声明,因为它会给'x'赋予不同的含义

if(true) { string var = "VAR"; } string var = "New VAR!"; 这将导致: 错误1名为'var'的局部变量不能在此范围内声明,因为它会给'var'赋予不同的含义,'var'已用于'子'范围内以表示其他内容。 没有什么真正的地球震碎,但这不是明显的错误? 一位开发人员和我想知道第一个声明是否应该在不同的范围内,因此第二个声明不能干扰第一个声明。 为什么C#无法区分这两个范围? 第一

Why can't I declare a variable with the same name in different scopes in C#?

This question already has an answer here: Why doesn't C# allow me to use the same variable name in different scopes? 5 answers Not every {} starts a new scope. The integer declared in the if block is still on the same stack as the function. From MSDN Compiler Error CS0136 For each occurrence of a given identifier as a simple-name in an expression or declarator, within the local vari

为什么我不能在C#中的不同范围内声明一个具有相同名称的变量?

这个问题在这里已经有了答案: 为什么C#不允许我在不同的作用域中使用相同的变量名? 5个答案 并非每个{}启动一个新的范围。 在if块中声明的整数与函数仍然在同一栈中。 从MSDN编译器错误CS0136 对于表达式或声明符中每个出现的给定标识符作为简单名称,在局部变量声明空间(§3.3)内立即包含该事件,在表达式或声明符中每隔一次出现与简单名称相同的标识符必须提及同一个实体。 该规则确保名称的含义在给定块,开关

Local variable scope error

I have the following code: if (Current == false) { foreach (var visual in visuals) visual.IsSelected = value; } Visual visual = visuals[currentIndex]; And when I compile I have this error: A local variable named 'visual' cannot be declared in this scope because it would give a different meaning to 'visual', which is already used in a 'child' scope to denot

局部变量范围错误

我有以下代码: if (Current == false) { foreach (var visual in visuals) visual.IsSelected = value; } Visual visual = visuals[currentIndex]; 而当我编译我有这个错误: 名为'visual'的局部变量不能在此范围内声明,因为它会给'visual'赋予不同的含义,'visual'已经在'child'范围内用于表示其他内容 另外,如果我不声明visual变量,那就是我替换: Visual visual = vi

Why doesn't C# allow me to use the same variable name in different scopes?

Like for instance: if ( this.IsValid ) { Matrix matrix = new Matrix(); } Matrix matrix = new Matrix(); The compiler warns me saying: "A local variable named ' matrix ' cannot be declared in this scope because it would give a different meaning to ' matrix ', which is already used in a 'child' scope to denote something else. Aren't these variables in diff

为什么C#不允许我在不同的作用域中使用相同的变量名?

例如: if ( this.IsValid ) { Matrix matrix = new Matrix(); } Matrix matrix = new Matrix(); 编译器警告我说: “名为' matrix '的局部变量不能在此范围内声明,因为它会给' matrix '赋予不同的含义,' matrix '已经在'子'范围内用于表示其他内容。 这些变量是不是在不同的范围内,所以我无法从if语句之外访问第一个matrix ? 更新:2011年以下的答案对于早期版本的C#是正确的

Using a variable name used in a child scope

This question already has an answer here: C# variable scoping: 'x' cannot be declared in this scope because it would give a different meaning to 'x' 3 answers It is a design choice made by the designers of C#. It reduces potential ambiguity. You can use it in one of the two places, inside the if or outside, but you can only define it in one place. Otherwise, you get a comp

使用子范围中使用的变量名称

这个问题在这里已经有了答案: C#变量范围:'x'不能在这个范围内声明,因为它会给'x'3个答案以不同的含义 它是C#设计人员设计的选择。 它减少了潜在的歧义。 你可以在if或outside之外的两个地方使用它,但你只能在一个地方定义它。 否则,您会发现编译器错误。 我注意到的东西在这里没有提到。 这将编译: for (int i = 0; i < 10; i++) { int a = i * 2; } for (int i = 0; i < 5; i++)

A local variable cannot be declared in this scope[Linq/Lambda expression]

I have following code snippet in c# static void Main() { var numbers = new[] { 1, 2, 3, 4, 5, 6 }; var ngt5 = numbers.Where(n => n > 5); var n = ngt5.First().ToString(); Console.WriteLine(n, numbers); } When I am compiling the above code I am getting following error A local variable named 'n' cannot be declared in this scope Your problem is here: // Within

局部变量不能在此范围内声明[Linq / Lambda表达式]

我在c#中有以下代码片段 static void Main() { var numbers = new[] { 1, 2, 3, 4, 5, 6 }; var ngt5 = numbers.Where(n => n > 5); var n = ngt5.First().ToString(); Console.WriteLine(n, numbers); } 当我编译上面的代码时,我收到以下错误 名为'n'的局部变量不能在此范围内声明 你的问题在这里: // Within your lambda you have an 'n'. var ngt5 = numbers.Where(n => n >

C#: yield return range/collection

I use the yield return keyword quite a bit, but I find it lacking when I want to add a range to the IEnumerable . Here's a quick example of what I would like to do: IEnumerable<string> SomeRecursiveMethod() { // some code // ... yield return SomeRecursiveMethod(); } Naturally this results in an error, which can be resolved by doing a simple loop. Is there a better way t

C#:收益率范围/收集

我使用yield return关键字相当多,但是当我想向IEnumerable添加一个范围时,我发现它缺乏。 下面是我想要做的一个快速示例: IEnumerable<string> SomeRecursiveMethod() { // some code // ... yield return SomeRecursiveMethod(); } 当然这会导致错误,可以通过简单的循环来解决。 有一个更好的方法吗? 一个循环感觉有点笨拙。 不,我不害怕。 F#支持这个yield! ,但在C#中没有等价物 - 基本

Is the List<T>.ForEach() method gone?

I started dabbling in Windows 8 metro recently, and found that one of my old buddies seems to have gone missing. I tend to use the .ForEach() method more than I use the traditional foreach() construct, and I realized pretty quickly that this method isn't available. For example, this code will not compile under a metro app: var list = new List<string>(); list.ForEach(System.Diagnost

List <T> .ForEach()方法消失了吗?

我最近开始涉足Windows 8地铁,发现我的一个老朋友似乎失踪了。 我倾向于使用.ForEach()方法,而不是使用传统的foreach()构造,并且我很快意识到这种方法不可用。 例如,这个代码不会在metro应用程序下编译: var list = new List<string>(); list.ForEach(System.Diagnostics.Debug.WriteLine); 我搜寻了一下,看看能否找到这方面的任何讨论,但无法做到。 我只是懒惰,还是实际上消失了? 它确实消失了: 列

Iterate with LINQ over a list and set founded varible

This question already has an answer here: LINQ equivalent of foreach for IEnumerable<T> 21 answers Why do you insist on linq? its not a good practice to use linq alll the time. How ever you can mix linq with foreach. foreach (var field in fieldExtension.Where(f => f.Name.Equals(item.Name))) { field.Access = true; }

在列表中迭代LINQ并设置创建的变量

这个问题在这里已经有了答案: LINQ相当于foreach for IEnumerable <T> 21个答案 你为什么坚持要linq? 它不是一个很好的习惯使用linq的时间。 你怎么能把linq和foreach混合起来。 foreach (var field in fieldExtension.Where(f => f.Name.Equals(item.Name))) { field.Access = true; }