我遇到了一些奇怪的事情,我不确定它是否是Visual Studio中的一个错误,或者我的无知可能是在欺骗我。
我有两个私有类变量:
class MyClass
{
private MyList<A> aList;
private MyList<B> bList;
[...]
在代码的某处,我第一次使用这些变量。
public void MyMethod()
{
object[] generatorOutput = Generator.Generate(args);
aList = (MyList<A>)generatorOutput[0];
bList = (MyList<B>)generatorOutput[1];
[...]
然而Visual Studio告诉我,bList是错误的:
Cannot use local variable 'bList' before it is declared.
The declaration of the local variable hides the Field 'MyNameSpace.MyClass.bList'.
我真的不明白Visual Studio的含义。我不希望bList是本地的,并且不应该隐藏任何内容。
如果有帮助:bList最初被称为cList,在我决定之前是MyList<C>
,MyList<B>
绰绰有余。只有在重命名变量并更改其类型后才会出现错误消息。顺便说一下,generatorOutput始终被转换为正确的类型。
那么,这是一个错误,还是我错过了一些明显的东西? 我已经尝试编译代码,重写行甚至重启Visual Studio而没有成功......
答案 0 :(得分:6)
我假设你的MyMethod
继续这样:
public void MyMethod()
{
object[] generatorOutput = Generator.Generate(args);
aList = (MyList<A>)generatorOutput[0];
bList = (MyList<B>)generatorOutput[1];
// ...
var bList = new MyList<B>(); // <---