在C#中的静态类不能声明实例成员 [英] cannot declare instance members in a static class in C#

查看:2352
本文介绍了在C#中的静态类不能声明实例成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公共静态类,我想从我的app.config文件访问的AppSettings在C#中,我得到上述错误

 公共静态类员工
{
的NameValueCollection appSetting = ConfigurationManager.AppSettings;

}



我如何得到这个工作?
PS:我粘贴代码,只需几行



感谢


解决方案<。 / DIV>

如果该类声明为static,所有成员都必须是静态的了。

 静态的NameValueCollection appSetting = ConfigurationManager.AppSettings; 



更新



足够重要,我在这里复制的注释:



@BoltClock有一个非常好的点;你确定你希望你的员工类是静态的?你几乎肯定不希望这种行为。你可能会被关闭从本类和成员的静态约束越好。


I have a Public Static Class and I am trying to access appsettings from my app.config file in C# and I get the above error

public static class employee
{
NameValueCollection appSetting = ConfigurationManager.AppSettings;    

}

How do I get this to work? PS: I pasted just a few lines of code.

thanks

解决方案

If the class is declared static, all of the members must be static too.

static NameValueCollection appSetting = ConfigurationManager.AppSettings;

Update

Important enough that I copied the comment here:

@BoltClock has a very good point; are you sure you want your employee class to be static? You almost certainly don't want that behaviour. You'd probably be better off removing the static constraint from the class and the members.

这篇关于在C#中的静态类不能声明实例成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆