Visual Studio 变量识别问题

Visual Studio issue with recognising variables

好吧,简而言之:

我声明一个变量,比如说

string str = "Random";

然后我尝试执行任何类型的操作,比方说

str.ToLower();

而且 visual studio 和 intellisense 都无法识别它。 VS 给我的名称 "str" 在当前上下文中不存在。这发生在我安装 xamarin 之后,但我不确定它是否相关。

如果我在一个方法中,也不会出现这个问题,只是当我直接在 class.

中时

这是我的代码:

public class Program {
   public void randomMethod() {
      string str2 = "Random";
      str.ToUpper(); //this line shows no errors
   }

   string str = "Random";
   str.ToLower(); //this line does show the error
}

str 会带有红色下划线,并且会出现上面提到的警告。

有人知道这是怎么回事吗?

你甚至自己指出问题

你不能这样做

public class Program {
   string str = "Random";
   str.ToLower(); //this line does show the error
}

您希望该代码何时 运行?

必须将可执行代码放入函数中。你指出这行得通。

我无法提出修复建议,因为我不知道您要做什么。

这是一个范围问题:

public class Program {
   public void randomMethod() { //method scope starts
      string str2 = "Random";
      str.ToUpper(); //this line shows no errors
   } //method scope ends

   string str = "Random";  //this is a class field, but is missing an accessibility level
   str.ToLower(); //this line SHOULD show an error, because you can't do this in a class
}