错误无法在静态类中声明实例成员

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.Specialized; using System.Configuration; 

我收到此错误

 cannot declare instance members in a static class 

在编写Static类时。 任何想法如何解决它如果可能保持类静态? 谢谢

 namespace XXX.Helpers { public static class Utility { public static string GetConfiguration(string section, string key) { NameValueCollection mySection = (NameValueCollection)ConfigurationManager.GetSection(section); return mySection[key]; } } } 

您无法在static类中定义非静态成员。 在静态类中声明实例成员是不合法的。

来自Static Classes and Static Class Members (C# Programming Guide)

以下列表提供了静态类的主要function:

  • 仅包含静态成员。

  • 无法实例化。

  • 是密封的。

  • 不能包含实例构造函数。

我不得不说,顺便说一句,我在静态类中看不到任何实例成员:)

无法在静态类中声明实例成员

出现此错误的原因是 –

静态类应该只包含静态成员