警告CS0168:声明变量'el'但从未使用过? [英] warning CS0168: The variable 'el' is declared but never used?
问题描述
假设我只想计算一个集合中的元素,所以我这样做
这个:
int i = 0;
foreach(MyClement el in MyCollection)
{
i ++;
}
返回i;
在2003编译器中是否有任何方法可以避免警告:
警告CS0168:变量''el''已声明但从未使用过?
我不想关闭警告(不能在VS 2003中)我想要
修复代码而不做任何浪费...
谢谢!
是的,我知道你可以得到
$ b的长度$ b collection ...我的真实例子比这里提供的要复杂得多
这里。
-Kelly
--------------------------------
来自:Kelly Anderson >
Suppose that I just want to count the elements in a collection, so I do
this:
int i = 0;
foreach (MyElement el in MyCollection)
{
i++;
}
return i;
Is there any way in the 2003 compiler to avoid the warning:
warning CS0168: The variable ''el'' is declared but never used?
I don''t want to turn off the warning (can''t anyway in VS 2003) I want
to fix the code without doing something wasteful...
Thanks!
And yes, I do know that you could just get the length of the
collection... my real example is a bit more complex than that presented
here.
-Kelly
--------------------------------
From: Kelly Anderson
推荐答案
使用for循环而不是foreach循环:
int co非= 0;
int i;
for(i = 0; I< MyCollection.Count; i ++){
count ++;
}
返回计数;
那个应该工作
use a for loop instead of a foreach loop:
int count = 0;
int i;
for (i = 0; i<MyCollection.Count; i++) {
count++;
}
return count;
that should work
你能提供一些关于真实例子的更多细节吗?
试着想一想在这种情况下,你想要迭代一个集合的成员,但实际上并没有对
个别成员做任何事情让我很头疼。如果你能给出更多的b $ b b细节,也许这会让我缺乏想象力。 :)
Could you give more details about the "real example"?
Trying to think of a situation in which you would want to iterate over
the members of a collection but not actually do anything with the
individual members is giving me a headache. If you could give more
details, perhaps it would help my dearth of imagination. :)
大声笑,多数民众赞成有趣。它开始让我头疼:)
lol, thats funny. it was starting to give me a headache as well :)
这篇关于警告CS0168:声明变量'el'但从未使用过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!