Skip to main content
 首页 » 编程设计

c#之"is a field and used like a type"错误

2024年02月13日11hnrainll

我正在尝试在 C# 类库项目中使用 StringBuilder。我已经添加了 using 声明和所有内容。我没有得到 intellisense,并且遇到了编译错误。

例如:

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

我在变量 sb 上看到一个错误,即

sb is a field and used like a type.

感谢任何帮助。

请您参考如下方法:

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

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 
  } 
} 

目前是这样的: