“是一个字段,像一个类型一样使用”错误

时间:2013-05-16 04:08:43

标签: c#

我正在尝试在C#类库项目中使用StringBuilder。我添加了using语句和所有语句。我没有得到intellisense,我收到编译错误。

例如:

StringBuilder sb= new StringBuilder();
sb.Append("");

我看到变量sb出错了,

  

sb是一个字段,用作类型。

感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

如果没有查看完整的代码,我只能猜测你是直接在类中编写代码,下一行应该在方法中

sb.Append("");

对于课程,它应该是:

using System.Text; //make sure this is included. 
public class MyClass
{
  StringBuilder sb = new StringBuilder();

  public MyClass() //Constructor
  {
   sb.Append(""); //this statement should be inside a method
  }
}

目前它会像:

enter image description here

答案 1 :(得分:0)

要使用StringBuilder类,您需要正确引用它的库。

using System.Text;添加到班级的顶部

相关问题